标签:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 19222 | Accepted: 7466 |
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!
#include<stdio.h>
#include<string.h>
#include<queue>
#define MAX 288
using namespace std;
int l,r,c;
char map[MAX][MAX][MAX];
struct node
{
int x,y,z;
int step;
};
void bfs(int x1,int y1,int z1,int x2,int y2,int z2)
{
int j,i,ok=0;
int move[6][3]={0,0,1,0,0,-1,1,0,0,-1,0,0,0,1,0,0,-1,0};
queue<node>q;
node beg,end;
beg.x=x1;
beg.y=y1;
beg.z=z1;
beg.step=0;
q.push(beg);
while(!q.empty())
{
end=q.front();
q.pop();
if(end.x==x2&&end.y==y2&&end.z==z2)
{
ok=1;
break;
}
for(i=0;i<6;i++)
{
beg.x=end.x+move[i][0];
beg.y=end.y+move[i][1];
beg.z=end.z+move[i][2];
if(0<=beg.x&&beg.x<l&&0<=beg.y&&beg.y<r&&0<=beg.z&&beg.z<c&&map[beg.x][beg.y][beg.z]!=‘#‘)
{
map[beg.x][beg.y][beg.z]=‘#‘;
beg.step=end.step+1;
q.push(beg);
}
}
}
if(ok)
printf("Escaped in %d minute(s).\n",end.step);
else
printf("Trapped!\n");
}
int main()
{
int i,j,k,x1,y1,z1,x2,y2,z2;
while(scanf("%d%d%d",&l,&r,&c)&&l!=0&&r!=0&&c!=0)
{
for(i=0;i<l;i++)
{
for(j=0;j<r;j++)
{
scanf("%s",map[i][j]);
}
}
for(i=0;i<l;i++)
for(j=0;j<r;j++)
for(k=0;k<c;k++)
{
if(map[i][j][k]==‘S‘)
{
x1=i;y1=j;z1=k;
}
else if(map[i][j][k]==‘E‘)
{
x2=i,y2=j;z2=k;
}
}
bfs(x1,y1,z1,x2,y2,z2);
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/tonghao/p/4593618.html