HDU2102 A计划
又一个三维迷宫!
问题链接:HDU2102 A计划。
问题简述:参见上述链接
问题分析:虽然是三维迷宫,其实只有两层,骑士进入迷宫营救公主,找到公主即可。迷宫的入口是S(0,0,0),公主位置为'P',时空传输机为'#'表示,墙为'*'表示,平地为'.'。层间移动只能通过时空传输机,并且不需要时间。骑士在同一层中只能前后左右移动,每移动一格花1时刻。输入n,有n组测试数据,每个测试数据有一行三个整数N、M和T以及迷宫数据,迷宫大小为N*M(1 <= N,M <=10),需要在T时刻前找到公主。问能否在限定时间内找到公主,能找到公主就输出“YES”,否则输出“NO”。
程序说明:(略)。
AC的C++语言程序如下:
/* HDU2102 A计划 */
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int DIRECTSIZE = 4;
struct direct {
int dy, dz;
} direct[DIRECTSIZE] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
const int MAXN = 10;
const int L = 2;
int M, N, limit;
struct node {
int x, y, z, level;
};
char cube[L][MAXN+2][MAXN+2];
int bfs()
{
node start;
start.x=0;
start.y=1;
start.z=1;
start.level=0;
queue<node> q;
q.push(start);
while(!q.empty())
{
node front = q.front();
q.pop();
if(cube[front.x][front.y][front.z] == 'P')
return 1;
else if(cube[front.x][front.y][front.z] == '*')
continue;
cube[front.x][front.y][front.z]='*';
for(int i=0; i<DIRECTSIZE; i++) {
node v;
v.x = front.x;
v.y = front.y + direct[i].dy;
v.z = front.z + direct[i].dz;
v.level = front.level + 1;
if(cube[v.x][v.y][v.z]=='*' || v.level > limit)
continue;
else if(cube[v.x][v.y][v.z] == '#') {
cube[v.x][v.y][v.z] = '*';
v.x = 1 - v.x;
if(cube[v.x][v.y][v.z]=='#' || cube[v.x][v.y][v.z]=='*') {
cube[v.x][v.y][v.z] = cube[1-v.x][v.y][v.z] = '*';
continue;
}
}
q.push(v);
}
}
return 0;
}
int main()
{
int t, ans;
cin >> t;
while(t--) {
cin >> M >> N >> limit;
memset(cube,'*',sizeof(cube));
for(int i=0; i<L; i++)
for(int j=1; j<=M; j++)
for(int k=1; k<=N; k++)
cin >> cube[i][j][k];
ans = bfs();
if(ans)
printf("YES\n");
else
printf("NO\n");
}
return 0;
}
浙公网安备 33010602011771号