题解:
求最短逃离迷宫时间,显然BFS,不过只是从前后左右变成了上下前后左右六个方向。
代码
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define MAX 100000
#define LL long long
int cas=1,T;
int l,m,n;
char mapp[35][35][35];
int vis[35][35][35];
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};
int to_dir[6][3]={{0,-1,0},{0,0,1},{0,1,0},{0,0,-1},{1,0,0},{-1,0,0}};
bool check(int x,int y,int h)
{
if (x<0 || x >= n || y<0 ||y>=m||h<0||h>=l||mapp[h][x][y]=='#'|| vis[h][x][y])
return false;
else
return true;
}
struct Node
{
int x;
int y;
int h;
int step;
};
queue<Node> que;
int main()
{
while (scanf("%d%d%d",&l,&n,&m))
{
Node s,e,t;
if (n==0)
break;
for (int i = 0;i<l;i++)
for (int j = 0;j<n;j++)
{
scanf("%s",&mapp[i][j]);
for (int k=0;k<m;k++)
{
if (mapp[i][j][k]=='S')
{
s.h=i;
s.x=j;
s.y=k;
s.step=0;
vis[i][j][k]=1;
}
if (mapp[i][j][k]=='E')
{
e.h=i;
e.x=j;
e.y=k;
e.step=0;
}
}
}
int flag = 0;
while (!que.empty())
que.pop();
que.push(s);
while (!que.empty())
{
t=que.front();
que.pop();
if (t.x==e.x && t.y==e.y&&t.h==e.h)
{
flag = 1;
printf("Escaped in %d minute(s).\n",t.step);
break;
}
for (int i = 0;i<6;i++)
{
Node q = t;
q.x +=to_dir[i][0];
q.y +=to_dir[i][1];
q.h +=to_dir[i][2];
if (!check(q.x,q.y,q.h))
continue;
q.step++;
vis[q.h][q.x][q.y]=1;
que.push(q);
}
}
if (!flag)
{
printf("Trapped!\n");
}
memset(mapp,0,sizeof(mapp));
memset(vis,0,sizeof(vis));
}
//freopen("in","r",stdin);
//scanf("%d",&T);
//printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC);
return 0;
}