uva 532 Dungeon Master
原題:
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 file 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!
中文:
給你一個三維迷宮,起點標成S,終點標成E,能走的地方是’.’,不能走的地方是’#’,現在問你從S走到E如果能走通,最少要走多少步,如果不能走通,輸出Trapped!
程式碼:
#include <bits/stdc++.h>
using namespace std;
struct node
{
int x,y,z;
int cnt;
node()
{
x= y=z=cnt=0;
}
node(int xx,int yy,int zz,int c)
{
x=xx,y=yy,z=zz,cnt=c;
}
};
int L,R,C;
char maze[31][31][31];
bool vis[31][31][31];
int Move[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};
bool edge(int x,int y,int z)
{
if(x>=1&&x<=L&&y>=1&& y<=R&&z>=1&&z<=C)
return true;
return false;
}
int bfs(node st,node en)
{
memset(vis,0,sizeof(vis));
queue<node> Q;
Q.push(st);
vis[st.x][st.y][st.z]=1;
while(!Q.empty())
{
node tmp=Q.front();
Q.pop();
for(int i=0;i<6;i++)
{
node res(tmp.x+Move[i][0],tmp.y+Move[i][1],tmp.z+Move[i][2],tmp.cnt+1);
if(res.x==en.x&&res.y==en.y&&res.z==en.z)
return res.cnt;
if(edge(res.x,res.y,res.z)&&!vis[res.x][res.y][res.z]&&maze[res.x][res.y][res.z]=='.')
{
Q.push(res);
vis[res.x][res.y][res.z]=1;
}
}
}
return -1;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>L>>R>>C,L+R+C)
{
node st,en;
for(int i=1;i<=L;i++)
{
for(int j=1;j<=R;j++)
{
for(int k=1;k<=C;k++)
{
cin>>maze[i][j][k];
if(maze[i][j][k]=='S')
{
st.x=i,st.y=j,st.z=k;
}
if(maze[i][j][k]=='E')
{
en.x=i,en.y=j,en.z=k;
}
}
}
}
int ans=bfs(st,en);
if(ans!=-1)
cout<<"Escaped in "<<ans<<" minute(s)."<<endl;
else
cout<<"Trapped!"<<endl;
}
return 0;
}
思路:
裸的廣搜,沒啥好說的~
相關推薦
uva 532 Dungeon Master
原題: 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 n
POJ 2251:Dungeon Master(三維BFS)
ota span pla south integer gen i++ break align Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16
POJ-2251 Dungeon Master
indicate ons posit cpp mem uic lac bre cap Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon
Dungeon Master (三維BFS)
follow 題目 not making clas ont orm 出發 nes 題目: You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is compos
Dungeon Master
urn minute class 測試數據 時間限制 解法 bfs struct 左右 Dungeon Master 鏈接:http://ybt.ssoier.cn:8088/problem_show.php?pid=1248 時間限制: 1000 ms
POJ2251 Dungeon Master
char 成了 queue struct sam col evel spa rmi B - Dungeon Master You are trapped in a 3D dungeon and need to find the quickest way out! The d
POJ 2251 Dungeon Master 三維bfs
struct scanf namespace \n AS OS algorithm 判斷 迷宮 題目:http://poj.org/problem?id=2251 題意:輸入一個L層的R*C迷宮,從S點出發,判斷能否到達E,如果能,輸出最短時間。 解法:簡單bfs,把四個方
Dungeon Master(三維bfs)
PE name sent pan empty time scan memset cape 題目鏈接:http://poj.org/problem?id=2251 題目: Description You are trapped in a 3D dungeon and n
Dungeon Master的兩種方法
優先隊列 lag pri HERE out space sta quick diag Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon
POJ 2251 Dungeon Master【三維BFS模板】
namespace orm 代碼 present 行數 integer ann fin HR Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 45743 Accepted:
棋盤問題(DFS)& Dungeon Master (DFS)
tro 所有 define ostream return sig form output des 1棋盤問題 在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請編程求解對於給定形狀和大小的棋盤
ZOJ 1940 Dungeon Master【三維BFS】
can amp pan printf == zoj 鏈接 cap con <題目鏈接> 題目大意: 在一個立體迷宮中,問你從起點走到終點的最少步數。 解題分析: 與普通的BFS基本類似,只需要給數組多加一維,並且走的時候多加 上、下這兩個方向就行。 #in
POJ - 2251 Dungeon Master (三維bfs)
/* 三維bfs */ #include <iostream> #include <cstdio> #include <cstring> #include <queue> using namespace std; const int max
Dungeon Master (DFS)
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 r
POJ 2251-Dungeon Master (三維BFS)
分析 三維BFS即可,只能上下東西南北6個方向走。 注意不只是‘.’可以走‘E’也可以,就是保證不越界的情況下除了‘#’都可以走 vis忘memset wa了一發。 AC Code #include<iostream> #include<cstdio&g
POJ2251 Dungeon Master BFS
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!http://blog.csdn.net/jiangjunshow 也歡迎大家轉載本篇文章。分享知識,造福人民,實現我們中華民族偉大復興!  
Dungeon Master BFS
Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42008  
C - Dungeon Master (第一道queue實現的BFS)
#include<cstdio>#include<queue>#include<cstring>using namespace std;char a[35][35][35];int vis[35][35][35];int ob[6][3]={{-1,0,0},{1,0,0}
POJ 2251 Dungeon Master (三維BFS)
題目連結:http://poj.org/problem?id=2251 Dungeon Master Time Limit: 1000MS Memory Limit: 65536K
POJ 2251 Dungeon Master(三維BFS)
題目看起來很厲害,實際上看懂了並不難,開一個三維的陣列,這裡需要注意的是第一維是高度,然後就是簡單的BFS了,還有不同就是三維的時候有六個方向可以走,在前後左右的基礎上多了一個向上和向下的走法,還有一個問題就是多個輸入樣例要注意每次都要初始化,我做的時候就因為這個WA了好幾次,最後在學姐的幫助下才改