标签:
Description
Input
Output
Escaped in x minute(s).
Trapped!
Sample Input
3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0
Sample Output
Escaped in 11 minute(s). Trapped!
这道题其实就是bfs的水题,2维改成3维了。
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
const int MAXN=33;
const int INF=0x3f3f3f;
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 Point
{
int x,y,z;
};
char map[MAXN][MAXN][MAXN];
int dis[MAXN][MAXN][MAXN];
int sx,sy,sz;
int ex,ey,ez;
int high,row,col;
int bfs();
int main()
{
while(cin>>high>>row>>col)
{
if(high==0&&row==0&&col==0)
break;
for(int i=0;i<high;i++)
for(int j=0;j<row;j++)
{
char s[50];
cin>>s; //刚开始直接cin>>map[j][k][i],,错了。
for(int k=0;k<col;k++)
{
map[j][k][i]=s[k];
if(map[j][k][i]==‘S‘)
{
sx=j;sy=k;sz=i;
}
else if(map[j][k][i]==‘E‘)
{
ex=j;ey=k;ez=i;
}
dis[j][k][i]=INF;
}
}
int ans=bfs();
if(ans==INF)
cout<<"Trapped!"<<endl;
else
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
}
return 0;
}
int bfs()
{
queue<Point>que;
Point start;
start.x=sx;
start.y=sy;
start.z=sz;
que.push(start);
dis[sx][sy][sz]=0;
while(que.size())
{
Point p=que.front();
que.pop();
if(p.x==ex&&p.y==ey&&p.z==ez)
break;
for(int i=0;i<6;i++)
{
Point np;
np.x=p.x+dx[i];
np.y=p.y+dy[i];
np.z=p.z+dz[i];
if(np.x>=0&&np.x<row&&np.y>=0&&np.y<col&&np.z>=0&&np.z<high&&map[np.x][np.y][np.z]!=‘#‘
&&dis[np.x][np.y][np.z]==INF)
{
que.push(np);
dis[np.x][np.y][np.z]=dis[p.x][p.y][p.z]+1;
}
}
}
return dis[ex][ey][ez];
}
标签:
原文地址:http://www.cnblogs.com/-maybe/p/4242532.html