poj 2251 < bfs 求3D迷宫出路>
2016-07-28 18:29
337 查看
Dungeon Master
Description
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and
the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the
exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
Trapped!
Sample Input
Sample Output
Source
Ulm Local 1997
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,h,jx,jy,jz;
char ma[40][40][40];
bool fafe[40][40][40];
int xiang[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;
}now,qian;
void bfs(int xx,int yy,int zz)
{
now.x=xx;now.y=yy;now.z=zz;now.step=0;
queue<node> que;que.push(now);
ma[xx][yy][zz]='#';
int ans=0;
while (!que.empty())
{
now=que.front();
que.pop();
if (now.x==jx&&now.y==jy&&now.z==jz)
{
ans=now.step;
break;
}
for (int i=0;i<6;i++)
{
int zx=now.x+xiang[i][0];
int zy=now.y+xiang[i][1];
int zz=now.z+xiang[i][2];
if (zx>=0&&zx<h&&zy>=0&&zy<n&&zz>=0&&zz<m&&ma[zx][zy][zz]!='#')
{
qian.x=zx;qian.y=zy;qian.z=zz;
qian.step=now.step+1;
ma[zx][zy][zz]='#';
que.push(qian);
}
}
}
if (ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
int main()
{
while (scanf("%d%d%d",&h,&n,&m),n+m+h)
{
int kx,ky,kz;
for (int i=0;i<h;i++)
{
for (int j=0;j<n;j++)
{
scanf("%s",ma[i][j]);
for (int k=0;k<m;k++)
{
if (ma[i][j][k]=='S')
{
kx=i;ky=j;kz=k;
}
if (ma[i][j][k]=='E')
{
jx=i;jy=j;jz=k;
}
}
}
}
bfs(kx,ky,kz);
}
return 0;
}
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 26407 | Accepted: 10275 |
You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of unit cubes which may or may not be filled with rock. It takes one minute to move one unit north, south, east, west, up or down. You cannot move diagonally and
the maze is surrounded by solid rock on all sides.
Is an escape possible? If yes, how long will it take?
Input
The input consists of a number of dungeons. Each dungeon description starts with a line containing three integers L, R and C (all limited to 30 in size).
L is the number of levels making up the dungeon.
R and C are the number of rows and columns making up the plan of each level.
Then there will follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock is indicated by a '#' and empty cells are represented by a '.'. Your starting position is indicated by 'S' and the
exit by the letter 'E'. There's a single blank line after each level. Input is terminated by three zeroes for L, R and C.
Output
Each maze generates one line of output. If it is possible to reach the exit, print a line of the form
Escaped in x minute(s).
where x is replaced by the shortest time it takes to escape.
If it is not possible to escape, print the line
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
代码:
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,h,jx,jy,jz;
char ma[40][40][40];
bool fafe[40][40][40];
int xiang[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;
}now,qian;
void bfs(int xx,int yy,int zz)
{
now.x=xx;now.y=yy;now.z=zz;now.step=0;
queue<node> que;que.push(now);
ma[xx][yy][zz]='#';
int ans=0;
while (!que.empty())
{
now=que.front();
que.pop();
if (now.x==jx&&now.y==jy&&now.z==jz)
{
ans=now.step;
break;
}
for (int i=0;i<6;i++)
{
int zx=now.x+xiang[i][0];
int zy=now.y+xiang[i][1];
int zz=now.z+xiang[i][2];
if (zx>=0&&zx<h&&zy>=0&&zy<n&&zz>=0&&zz<m&&ma[zx][zy][zz]!='#')
{
qian.x=zx;qian.y=zy;qian.z=zz;
qian.step=now.step+1;
ma[zx][zy][zz]='#';
que.push(qian);
}
}
}
if (ans)
printf("Escaped in %d minute(s).\n",ans);
else
printf("Trapped!\n");
}
int main()
{
while (scanf("%d%d%d",&h,&n,&m),n+m+h)
{
int kx,ky,kz;
for (int i=0;i<h;i++)
{
for (int j=0;j<n;j++)
{
scanf("%s",ma[i][j]);
for (int k=0;k<m;k++)
{
if (ma[i][j][k]=='S')
{
kx=i;ky=j;kz=k;
}
if (ma[i][j][k]=='E')
{
jx=i;jy=j;jz=k;
}
}
}
}
bfs(kx,ky,kz);
}
return 0;
}
相关文章推荐
- VS使用命令行参数
- 从零开始搭建JAVA编程环境
- 轻松把玩HttpClient之设置代理,可以访问FaceBook
- 2016多校联合第四场 HDU5768
- tensorflow源码例子mnist源码——fully_connected_feed.py
- 轻松把玩HttpAsyncClient之模拟post请求示例
- GCC在C语言中内嵌汇编 asm __volatile__
- Maven的配置文件pom.xml
- tornado RequestHandler request.body & request.arguments
- [转]30分钟学会反向Ajax
- rehat 出现GDB debuginfo-install 问题处理
- 基于稀疏和HOG特征的人耳识别(代码)
- Unity树形结构
- 采用Spring管理Bean和依赖注入
- tensorflow源码例子mnist源码——mnist.py
- 【POJ】2251 - Dungeon Master(dfs + 队列)
- NOI2014 CodeVS3319 Bzoj3670 动物园
- Java世界的HTML转PDF调研
- TEMP
- dfs练习