题意:
从S到E,一个三维的BFS可以解决,多一维,可以这样去理解,二维相当于一个平面,而三维的相当于一栋楼房,只是在z上多了几个平面而已
另外,第一次用队列写BFS好开心,以前都是模拟队列,也是醉了
16ms
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
using namespace std;
int dx[]= {0,1,0,-1,0,0};
int dy[]= {-1,0,1,0,0,0};
int dz[]= {0,0,0,0,1,-1};
int n, m, t;
char mp[35][35][35];
int vis[35][35][35];
int qx, qy, sx,sy,qz,sz;
struct node
{
int x;
int y;
int z;
int step;
} now,next;
void bfs()
{
queue<node> que;
now.x = qx;
now.y = qy;
now.z = qz;
now.step = 0;
que.push(now);
int i;
vis[qz][qx][qy] = 1;
while(!que.empty())
{
now = que.front();
que.pop();
int x, y, z, step;
x = now.x;
y = now.y;
z = now.z;
step = now.step;
for(i = 0; i < 6; i++)
{
next.x = x+dx[i];
next.y = y+dy[i];
next.z = z+dz[i];
next.step = step+1;
if(next.x >= 0 && next.y >= 0 && next.z >= 0&&
next.x < n &&next.y < m && next.z <t&&
!vis[next.z][next.x][next.y]&&mp[next.z][next.x][next.y]!='#')
{
if(next.x == sx && next.y == sy && next.z == sz)
{
printf("Escaped in %d minute(s).\n",step+1);
return;
}
vis[next.z][next.x][next.y] = 1;
que.push(next);
}
}
}
printf("Trapped!\n");
}
int main()
{
while(~scanf("%d%d%d",&t,&n,&m)&&(t+n+m))
{
int i, j, k;
memset(mp,0,sizeof(mp));
memset(vis,0,sizeof(vis));
for(k = 0; k < t; k++)
for(i = 0; i < n; i++)
{
scanf("%s",mp[k][i]);
for(j = 0; j < m; j++)
{
if(mp[k][i][j] == 'S')
{
qx = i;
qy = j;
qz = k;
}
if(mp[k][i][j] == 'E')
{
sx = i;
sy = j;
sz = k;
}
}
}
bfs();
}
return 0;
}