poj 2251 Dungeon Master 题目链接:http://poj.org/problem?id=2251
三维BFS水
题目大意:这是一个三维地牢,你怎么出去呢?
题目分析:毫无tricker,一遍水过,不必加visit数组,直接破坏原地图就可以,输入要注意吃掉空行。
code:
#include<cstdio>
#include<queue>
using namespace std;
char map[40][40][40];
int m,n,p,sx,sy,sz,ex,ey,ez;
int dir[6][3]={1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};
struct node
{
int x,y,z,step;
};
bool judge(int x,int y,int z)
{
if(x>=0&&x<m&&y>=0&&y<n&&z>=0&&z<p&&map[x][y][z]!='#')return true;
else return false;
}
int bfs(int x,int y,int z)
{
queue<node>que;
node first,next;
first.x=x,first.y=y,first.z=z,first.step=0;
que.push(first);
while(!que.empty())
{
first=que.front();
que.pop();
for(int i=0;i<6;i++)
{
next.x=first.x+dir[i][0];
next.y=first.y+dir[i][1];
next.z=first.z+dir[i][2];
next.step=first.step+1;
if(judge(next.x,next.y,next.z))
{
que.push(next);
map[next.x][next.y][next.z]='#';
//printf("现在在:map[%d][%d][%d],step是%d,ex==%d,ey==%d,ez==%d\n",next.x,next.y,next.z,next.step,ex,ey,ez);
if(next.x==ex&&next.y==ey&&next.z==ez)return next.step;
}
}
}
return 0;
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&m,&n,&p)!=EOF&&(m||n||p))
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
getchar();
scanf("%s",map[i][j]);
for(k=0;k<p;k++)
{
if(map[i][j][k]=='S')sx=i,sy=j,sz=k;
else if(map[i][j][k]=='E')ex=i,ey=j,ez=k;
}
}
}
int flag=bfs(sx,sy,sz);
if(flag)printf("Escaped in %d minute(s).\n",flag);
else printf("Trapped!\n");
}
return 0;
}
PS:好久没这么顺了,搜索大概可以告一段落了,回归DP的干活!!