标签:bfspan idtransmarksp pspan idtransmarkspa
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 21312 | Accepted: 8283 |
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!
Source
Ulm Local 1997给出一三维空间的地牢,要求求出由字符‘S‘到字符‘E‘的最短路径
移动方向可以是上,下,左,右,前,后,六个方向
每移动一次就耗费一分钟,要求输出最快的走出时间。#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
using namespace std;
struct node
{
int x,y,z;
}p,s,e,q;
queue<node>Q;
int n,m,h;
char a[100][100][100]; //三维
int vis[100][100][100];
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 step[100][100][100];
int Judge(node q)
{
if(q.x>=0&&q.y>=0&&q.z>=0&&q.x<n&&q.y<m&&q.z<h&&a[q.x][q.y][q.z]!='#') //存在E
return 1;
return 0;
}
int BFS()
{
while(!Q.empty()) // <span id="transmark"></span>队列必须清空
Q.pop();
Q.push(s);
vis[s.x][s.y][s.z]=1;
step[s.x][s.y][s.z]=0;
while(!Q.empty())
{
p=Q.front();
Q.pop();
for(int i=0;i<6;i++)
{
q.x=dx[i]+p.x;
q.y=dy[i]+p.y;
q.z=dz[i]+p.z;
if(!Judge(q))
continue;
if(vis[q.x][q.y][q.z]==0)
{
Q.push(q);
step[q.x][q.y][q.z]=step[p.x][p.y][p.z]+1;
vis[q.x][q.y][q.z]=1;
}
if(a[q.x][q.y][q.z]=='E')
return step[q.x][q.y][q.z];
}
}
return -1;
}
int main()
{
while(~scanf("%d%d%d",&n,&m,&h))
{
int flag=1;
if(!n&&!m&&!h)
break;
memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
scanf("%s",a[i][j]);
if(flag)
for(int k=0;k<h;k++)
{
if(a[i][j][k]=='S')
{
s.x=i;s.y=j;s.z=k;
flag=0;
break;
}
}
}
}
int x=BFS();
if(x==-1)
{
printf("Trapped!\n");
continue;
}
printf("Escaped in %d minute(s).\n",x);
}
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:bfspan idtransmarksp pspan idtransmarkspa
原文地址:http://blog.csdn.net/became_a_wolf/article/details/48000499