POJ 2251 Dungeon Master
2016-07-28 19:25
447 查看
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
非常非常标准的bfs,很水,比较适合初学者。
注意x,y,z要一一对应=。=我对应的比较奇怪,具体还是看个人习惯吧……
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int L,R,C;
int sx,sy,sz,ex,ey,ez;
char map[35][35][35];
int dis[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 friend operator<(node a,node b)
{
return a.step>b.step;
}
}a,temp;
int OK(node a)
{
if(map[a.z][a.x][a.y]!='#'&&a.x>=0&&a.y>=0&&a.z>=0&&a.z<L&&a.x<R&&a.y<C)
return 1;
return 0;
}
int bfs()
{
a.x=sx,a.y=sy,a.z=sz;a.step=0;
priority_queue<node> q;
int i,n;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
return a.step;
for(i=0;i<6;i++)
{
temp.x=a.x+dis[i][0];
temp.y=a.y+dis[i][1];
temp.z=a.z+dis[i][2];
if(OK(temp))
{
temp.step=a.step+1;
map[temp.z][temp.x][temp.y]='#';
q.push(temp);
}
}
}
return -1;
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&L,&R,&C),L||R||C)
{
for(i=0;i<L;i++)
{
for(j=0;j<R;j++)
{
scanf("%s",map[i][j]);
for(k=0;k<C;k++)
{
if(map[i][j][k]=='S')
{
sx=j;sy=k;sz=i;
}
if(map[i][j][k]=='E')
{
ex=j;ey=k;ez=i;
}
}
}
getchar();
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
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
非常非常标准的bfs,很水,比较适合初学者。
注意x,y,z要一一对应=。=我对应的比较奇怪,具体还是看个人习惯吧……
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
int L,R,C;
int sx,sy,sz,ex,ey,ez;
char map[35][35][35];
int dis[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 friend operator<(node a,node b)
{
return a.step>b.step;
}
}a,temp;
int OK(node a)
{
if(map[a.z][a.x][a.y]!='#'&&a.x>=0&&a.y>=0&&a.z>=0&&a.z<L&&a.x<R&&a.y<C)
return 1;
return 0;
}
int bfs()
{
a.x=sx,a.y=sy,a.z=sz;a.step=0;
priority_queue<node> q;
int i,n;
q.push(a);
while(!q.empty())
{
a=q.top();
q.pop();
if(a.x==ex&&a.y==ey&&a.z==ez)
return a.step;
for(i=0;i<6;i++)
{
temp.x=a.x+dis[i][0];
temp.y=a.y+dis[i][1];
temp.z=a.z+dis[i][2];
if(OK(temp))
{
temp.step=a.step+1;
map[temp.z][temp.x][temp.y]='#';
q.push(temp);
}
}
}
return -1;
}
int main()
{
int i,j,k;
while(scanf("%d%d%d",&L,&R,&C),L||R||C)
{
for(i=0;i<L;i++)
{
for(j=0;j<R;j++)
{
scanf("%s",map[i][j]);
for(k=0;k<C;k++)
{
if(map[i][j][k]=='S')
{
sx=j;sy=k;sz=i;
}
if(map[i][j][k]=='E')
{
ex=j;ey=k;ez=i;
}
}
}
getchar();
}
int ans=bfs();
if(ans==-1)
printf("Trapped!\n");
else
printf("Escaped in %d minute(s).\n",ans);
}
return 0;
}
相关文章推荐
- Eclipse代码注释模板
- TCP的状态 (SYN, FIN, ACK, PSH, RST, URG)
- 安卓版本更新的简单Demo
- MATLAB弹出窗口定义操作函数
- 系统io各个函数用法总结
- Linux基础知识之文件的时间戳及touch的使用
- jabber服务器搭建
- Combining Sketch and Tone for Pencil Drawing Production
- Red and Black dfs水题
- 第三方登录(Weixin)
- 陀螺仪简单使用
- git 删除未跟踪文件
- 死锁
- C语言实现:获取ifconfig相关参数
- Unable to execute dex: method ID not in [0, 0xffff
- HDU Robberies
- Eclipse安装SVN教程
- 习题 4-4 骰子涂色(Cubepainting,UVa 253)
- C语言0728
- 编译内核