题目大意:
三维的迷宫问题
思路:
与二维的类似仍用BFS求解,只不过入口由四个变为六个。
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
struct node
{
int x, y, z, step;
};
int dx[] = {0,0,-1,1,0,0};
int dy[] = {1,-1,0,0,0,0};
int dz[] = {0,0,0,0,1,-1};
char str[35][35][35];
int vis[35][35][35];
int n, row, col;
node start, endd;
int check(node a)
{
if (a.x >= 0 && a.x < n && a.y >= 0 && a.y <row && a.z >= 0 && a.z < col && str[a.x][a.y][a.z] == '.' && !vis[a.x][a.y][a.z])
return 1;
return 0;
}
void BFS()
{
queue <node> q;
q.push(start);
while (!q.empty())
{
node cur = q.front();
q.pop();
for (int i = 0; i != 6; ++i)
{
node next;
next.x = cur.x + dz[i];
next.y = cur.y + dx[i];
next.z = cur.z + dy[i];
if (next.x == endd.x && next.y == endd.y&& next.z== endd.z)
{
endd.step = cur.step + 1;
return ;
}
if (check(next))
{
next.step = cur.step+1;
vis[next.x][next.y][next.z] = 1;
q.push(next);
}
}
}
}
int main()
{
while (cin >> n >> row >> col && n)
{
string t;
for (int i = 0; i != n; ++i)
{
for (int j = 0; j != row; ++j)
{
scanf("%s",str[i][j]);
for (int k = 0; k != col; ++k)
if (str[i][j][k] == 'S')
start = {i,j,k,0};
else if (str[i][j][k] == 'E')
endd = {i,j,k,0};
}
getline(cin,t); //读取每一组数据后的空行
}
memset(vis,0,sizeof(vis));
BFS();
if (endd.step)
printf("Escaped in %d minute(s).\n",endd.step);
else
cout << "Trapped!" << endl;
}
return 0;
}