题意
给出一个三维空间迷宫,“.”代表可行走的空间,“#”代表不可行走的封闭间。从"S"开始,求到地点“E”的最短时间。
每次移动可在迷宫内上下左右前后移动。
样例
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
AC代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
char map[35][35][35];
int vis[35][35][35];
int k,n,m,sx,sy,sz,ex,ey,ez;
int dx[6]={-1,1,0,0,0,0};
int dy[6]={0,0,-1,1,0,0};
int dz[6]={0,0,0,0,1,-1};
struct node
{
int x,y,z,step;
};
int check(int x,int y,int z)
{
if(x<0||y<0||z<0||x>=k||y>=n||z>=m||map[x][y][z]=='#'||vis[x][y][z])
return 1;
return 0;
}
//每次去队首元素,如果是终点则输出结果移动的次数,
//否则,从该点开始分别向东南西北上下移动(如果可以走的话)并继续搜,如果到队列为空还没搜到解法,则说明无解。
int bfs()
{
node a,next;
queue<node> Q;
a.x=sx;
a.y=sy;
a.z=sz;
a.step=0;
vis[sx][sy][sz]=1;
Q.push(a);
while(!Q.empty())
{
a=Q.front();
Q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
return a.step;
for(int i=0;i<6;++i)
{
next=a;
next.x=a.x+dx[i];
next.y=a.y+dy[i];
next.z = a.z+dz[i];
if(check(next.x,next.y,next.z))
continue;
vis[next.x][next.y][next.z]=1;
next.step=a.step+1;
Q.push(next);
}
}
return 0;
}
int main()
{
while(scanf("%d%d%d",&k,&n,&m),n+m+k)
{
for(int i=0;i<k;++i)
{
for(int j=0;j<n;++j)
{
scanf("%s",map[i][j]);
for(int r=0;r<m;++r)
{
if(map[i][j][r]=='S')
{
sx=i;
sy=j;
sz=r;
}
else if(map[i][j][r]=='E')
{
ex=i;
ey=j;
ez=r;
}
}
}
}
memset(vis,0,sizeof(vis));
int ans;
ans=bfs();
if(ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
return 0;
}
题源:poj.org/problem?id=…