题目链接:http://poj.org/problem?id=2251
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 16682 | Accepted: 6491 |
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
题解:(1)这道题比较简单,但是比较繁琐,要注意的细节比较多,=。=(2)广搜用的队列,求两点之间的距离一定是最短的~
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<string>
#include<stack>
#include<cmath>
#include<cctype>
#include<iostream>
#include<set>
#include<algorithm>
#include<ctime>
#include<vector>
using namespace std;
#define judge(x,y,z) !vis[x][y][z]&&map[x][y][z]!='#'&&x>=1&&x<=l&&y>=1&&y<=m&&z>=1&&z<=n
//设置方向数组优化广搜
int xx[]={0,0,0,0,1,-1};
int yy[]={-1,1,0,0,0,0};
int zz[]={0,0,-1,1,0,0};
bool vis[35][35][35];
char map[35][35][35];//地图
int l,m,n;
//设置结构体
struct node
{
int x,y,z;
int step;
};
int BFS(int x,int y,int z)
{
memset(vis,0,sizeof(vis));
queue<node>q;
node u;
u.x=x;
u.y=y;
u.z=z;
u.step=0;
q.push(u);
while(!q.empty())
{
u=q.front();
if(map[u.x][u.y][u.z]=='E')return u.step;
q.pop();
for(int i=0;i<6;i++)
{
node v;
v.x=u.x+xx[i];
v.y=u.y+yy[i];
v.z=u.z+zz[i];
if(judge(v.x,v.y,v.z))
{
vis[v.x][v.y][v.z]=true;
v.step=u.step+1;
q.push(v);
}
}
}
return 0;
}
int main()
{
while(cin>>l>>m>>n)
{
memset(map,0,sizeof(map));
if(!m&&!n&&!l)break;
int sx,sy,sz;
for(int i=1;i<=l;i++)
for(int j=1;j<=m;j++)
for(int k=1;k<=n;k++)
{
cin>>map[i][j][k]; //这里注意一下:表示第i层第j行第k列
if(map[i][j][k]=='S')
{
sx=i;
sy=j;
sz=k;
}
}
int step=BFS(sx,sy,sz);
if(step)printf("Escaped in %d minute(s).\n",step);
else printf("Trapped!\n");
}
return 0;
}
原文地址:http://blog.csdn.net/liusuangeng/article/details/38960591