poj 2251 Dungeon Master
poj Master
2023-09-27 14:27:21 时间
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 3 4 5 S.... .###. .##.. ###.# ##### ##### ##.## ##... ##### ##### #.### ####E 1 3 3 S## #E# ### 0 0 0 Sample Output Escaped in 11 minute(s). Trapped! Source
一道简单的BFS仅仅是方向改为6个 平面改为立体 定义三维数组就可以 要求输出最短时间(没走一步加时一分钟 )及最短路径。。
|
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int ans,n,c,r;
int sh,sx,sy;
int eh,ex,ey;
typedef class{
public:
int h,r,c;
int depth;
}ZB;
int map[40][40][40],vis[40][40][40];
int bfs(int k,int x,int y)
{
ZB queue[30000];
int head,tail;
queue[head=0].h=k;
queue[tail=0].r=x;
queue[0].c=y;
queue[tail++].depth=1;
vis[k][c][y]=1;
while(head<tail)
{
ZB w=queue[head++];
if(w.h==eh&&w.r==ex&&w.c==ey)
{
ans=w.depth;
return 1;
}
if(map[w.h][w.r][w.c-1]&&!vis[w.h][w.r][w.c-1])//西
{
vis[w.h][w.r][w.c-1]=1;
queue[tail].h=w.h;
queue[tail].r=w.r;
queue[tail].c=w.c-1;
queue[tail++].depth=w.depth+1;
}
if(map[w.h][w.r-1][w.c]&&!vis[w.h][w.r-1][w.c])//北
{
vis[w.h][w.r-1][w.c]=1;
queue[tail].h=w.h;
queue[tail].r=w.r-1;
queue[tail].c=w.c;
queue[tail++].depth=w.depth+1;
}
if(map[w.h][w.r][w.c+1]&&!vis[w.h][w.r][w.c+1])//东
{
vis[w.h][w.r][w.c+1]=1;
queue[tail].h=w.h;
queue[tail].r=w.r;
queue[tail].c=w.c+1;
queue[tail++].depth=w.depth+1;
}
if(map[w.h][w.r+1][w.c]&&!vis[w.h][w.r+1][w.c])//南
{
vis[w.h][w.r+1][w.c]=1;
queue[tail].h=w.h;
queue[tail].r=w.r+1;
queue[tail].c=w.c;
queue[tail++].depth=w.depth+1;
}
if(map[w.h+1][w.r][w.c]&&!vis[w.h+1][w.r][w.c])//下
{
vis[w.h+1][w.r][w.c]=1;
queue[tail].h=w.h+1;
queue[tail].r=w.r;
queue[tail].c=w.c;
queue[tail++].depth=w.depth+1;
}
if(map[w.h-1][w.r][w.c]&&!vis[w.h-1][w.r][w.c])//上
{
vis[w.h-1][w.r][w.c]=1;
queue[tail].h=w.h-1;
queue[tail].r=w.r;
queue[tail].c=w.c;
queue[tail++].depth=w.depth+1;
}
}
return 0;
}
int main()
{
int i,j,k;
char wz;
while(cin>>n>>r>>c,n&&r&&c)
{
memset(vis,0,sizeof(vis));
memset(map,0,sizeof map);
for(k=1;k<=n;k++)
for(i=1;i<=r;i++)
for(j=1;j<=c;j++)
{
cin>>wz;
if(wz=='S')
{
map[k][i][j]=1;
sh=k;sx=i;sy=j;
}
else if(wz=='E')
{
map[k][i][j]=1;
eh=k;ex=i;ey=j;
}
else if(wz=='.')
map[k][i][j]=1;
}
/* for(k=0;k<n;k++)
{for(i=0;i<r;i++)
{for(j=0;j<c;j++)
cout<<map[k][i][j];
cout<<endl;
}
cout<<endl;
}*/
if(bfs(sh,sx,sy))
cout<<"Escaped in "<<ans-1<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}
相关文章
- 【POJ】3070 Fibonacci(矩阵乘法)
- 【POJ】2828 Buy Tickets(线段树+特殊的技巧/splay)
- 【POJ】2104 K-th Number(区间k大+主席树)
- POJ 3678 Katu Puzzle (经典2-Sat)
- POJ 3648 Wedding(2-sat)
- POJ 2251 Dungeon Master
- Wormholes POJ 3259【SPFA】
- POJ 1068 Parencodings (模拟),暑假第一题~
- POJ - 3522 最小生成树
- Strategic game POJ - 1463 dfs
- POJ 1470 Closest Common Ancestors (LCA,离线Tarjan算法)
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
- 【POJ 1741】Tree
- poj 3013 Big Christmas Tree (dij+优先级队列优化 求最短)
- 【POJ 3311】Hie with the Pie(状压DP)