您的位置:首页 > 其它

POJ 2251 - Dungeon Master

2012-10-25 13:18 232 查看
题目大意:在一个3D的地牢中,你需要尽快逃出去,给定一个场景,要求出是否能够逃出去,如果可以,则输出至少需要多长时间,否则输出Trapped;每次可以走的方向前、后、左、右、上、下;
思路: 一个三维的BFS,每次如果当前位置可以向某一方向前进的话,则让前进后的位置入队,时间+1;在队列非空之前如果能找到出口则输出时间,否则不能出来;

code:
#include<cstdio>
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
struct node
{
int x,y,z,step;
} p,q;
int direct[6][3]= {{1,0,0},{-1,0,0},{0,1,0},{0,-1,0},{0,0,1},{0,0,-1}};
bool flag[35][35][35];
char map[35][35][35];
int x,y,z;
bool judge(int xi,int yi,int zi)
{
if(xi>=0&&xi<x&&yi>=0&&yi<y&&zi>=0&&zi<z&&!flag[xi][yi][zi]&&map[xi][yi][zi]!=\'#\')
return true;
return false;
}
int xx,yy,zz;
void bfs()
{
memset(flag,false,sizeof(flag));
queue<node> Q;
p.x=xx;
p.y=yy;
p.z=zz;
p.step=0;
flag[xx][yy][zz]=true;
Q.push(p);
while(!Q.empty())
{
p=Q.front();
Q.pop();
if(map[p.x][p.y][p.z]==\'E\')
{
cout<<\"Escaped in \"<<p.step<<\" minute(s).\"<<endl;
return ;
}
for(int i=0; i<6; i++)
{
q.x=p.x+direct[i][0];
q.y=p.y+direct[i][1];
q.z=p.z+direct[i][2];
if(judge(q.x,q.y,q.z))
{
q.step=p.step+1;
Q.push(q);
flag[q.x][q.y][q.z]=true;
}
}
}
cout<<\"Trapped!\"<<endl;
}
int main()
{
//freopen(\"in.txt\",\"r\",stdin);
//freopen(\"out.txt\",\"w\",stdout);
while(cin>>x>>y>>z &&(x||y||z))
{
memset(map,\'#\',sizeof(map));
for(int i=0; i<x; i++)
{
if(i)
getchar();
for(int j=0; j<y; j++)
{
getchar();
for(int k=0; k<z; k++)
{
char c=getchar();
map[i][j][k]=c;
if(c==\'S\')
{
xx=i;
yy=j;
zz=k;
}
}
}
}
bfs();
}
return 0;
}
/*
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: