Dungeon Master<poj2251>
2016-07-29 08:49
323 查看
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
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[40][40][40];
int vis[40][40][40];
struct node
{
int ceng,hang,lie;
int step;
};
int main()
{
int l,n,m;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
if(l==0&&n==0&&m==0)
break;
memset(vis,0,sizeof(vis));
struct node start;
for(int i=0;i<l;i++)
{
for(int j=0;j<n;j++)
{
scanf("%s",&map[i][j]);
for(int k=0;k<m;k++)
{
if(map[i][j][k]=='S')
{
start.ceng =i;
start.hang =j;
start.lie =k;
start.step =0;
vis[i][j][k]=1;
}
}
}
}
queue<node>bfs;
bfs.push (start);
int flog=0;
while(!bfs.empty ())
{
struct node tmp=bfs.front ();
bfs.pop ();
if(map[tmp.ceng ][tmp.hang ][tmp.lie ]=='E')
{
flog=1;
printf("Escaped in %d minute(s).\n",tmp.step);
break;
}
tmp.step ++;
struct node tmp2;
if(tmp.ceng +1<l)
{
tmp2=tmp;
tmp2.ceng ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.ceng -1>=0)
{
tmp2=tmp;
tmp2.ceng--;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.hang +1<n)
{
tmp2=tmp;
tmp2.hang ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.hang -1>=0)
{
tmp2=tmp;
tmp2.hang --;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.lie +1<m)
{
tmp2=tmp;
tmp2.lie ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push(tmp2);
}
}
if(tmp.lie -1>=0)
{
tmp2=tmp;
tmp2.lie --;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
}
if(flog==0)
printf("Trapped!\n");
}
return 0;
}
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
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
char map[40][40][40];
int vis[40][40][40];
struct node
{
int ceng,hang,lie;
int step;
};
int main()
{
int l,n,m;
while(scanf("%d%d%d",&l,&n,&m)!=EOF)
{
if(l==0&&n==0&&m==0)
break;
memset(vis,0,sizeof(vis));
struct node start;
for(int i=0;i<l;i++)
{
for(int j=0;j<n;j++)
{
scanf("%s",&map[i][j]);
for(int k=0;k<m;k++)
{
if(map[i][j][k]=='S')
{
start.ceng =i;
start.hang =j;
start.lie =k;
start.step =0;
vis[i][j][k]=1;
}
}
}
}
queue<node>bfs;
bfs.push (start);
int flog=0;
while(!bfs.empty ())
{
struct node tmp=bfs.front ();
bfs.pop ();
if(map[tmp.ceng ][tmp.hang ][tmp.lie ]=='E')
{
flog=1;
printf("Escaped in %d minute(s).\n",tmp.step);
break;
}
tmp.step ++;
struct node tmp2;
if(tmp.ceng +1<l)
{
tmp2=tmp;
tmp2.ceng ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.ceng -1>=0)
{
tmp2=tmp;
tmp2.ceng--;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.hang +1<n)
{
tmp2=tmp;
tmp2.hang ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.hang -1>=0)
{
tmp2=tmp;
tmp2.hang --;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
if(tmp.lie +1<m)
{
tmp2=tmp;
tmp2.lie ++;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push(tmp2);
}
}
if(tmp.lie -1>=0)
{
tmp2=tmp;
tmp2.lie --;
if(map[tmp2.ceng][tmp2.hang][tmp2.lie]!='#'&&vis[tmp2.ceng][tmp2.hang][tmp2.lie]==0)
{
vis[tmp2.ceng][tmp2.hang][tmp2.lie]=1;
bfs.push (tmp2);
}
}
}
if(flog==0)
printf("Trapped!\n");
}
return 0;
}
相关文章推荐
- POJ 2251 Dungeon Master <三维BFS水题>
- poj&nbsp;2251&nbsp;Dungeon&nbsp;Master
- POJ-2251 Dungeon Master bfs搜索
- poj1061 青蛙的约会 && poj 2115 C Looooops<扩展欧几里得>
- poj Dungeon Master(Bfs)
- POJ 2752(不满足P[i]<>P[next[i]] 的next函数)
- poj 1014 Dividing <多重背包>
- Dungeon Master&&http://poj.org/problem?id=2251
- POJ 2251 Dungeon Master BFS
- POJ 1451 T9 <Trie> 找出现频率最高的字符串
- Dungeon Master--POJ 2251
- <poj1028> Web Navigation
- DP <裸> POJ 1157
- poj1006 Biorhythms<中国剩余定理>
- <poj 1046>Color Me Less
- 刘汝佳<算法艺术与信息学竞赛>推荐题目——Poj
- ASP.NET MVC 关于MasterPage中<script>标签的文件路径自动解析
- <poj,sicily>Anti-prime Sequences (DFS)
- poj 1845 Sumdiv <数论>
- poj1037 A decorative fence<DP>