POJ 2251 Dungeon Master
POJ 2251 Dungeon Master
题目链接http://poj.org/problem?id=2251
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 lineTrapped!
Sample Input
3 4 5
S....
.###.
.##..
###.#
#####
#####
##.##
##...
#####
#####
#.###
####E
1 3 3
S##
#E#
###
0 0 0
Sample Output
Escaped in 11 minute(s).
Trapped!
题意:
就是给你一个三维矩阵,起点和终点。询问能否从起点走到终点。入股可以输出步数,不可以则是其他的结果。
题解:
裸的搜索。我用bfs过掉了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
struct dre{
int l,r,c;
int dep;
};
dre s,e;
bool vis[40][40][40];
int L,R,C;
int bfs(dre st,dre en){
queue <dre> q;
q.push(st);
while (!q.empty()){
dre temp = q.front();
q.pop();
if (temp.l == en.l && temp.r == en.r && temp.c == en.c )
return temp.dep;
temp.dep++;
dre temp2;
//down
if (temp.l > 1){
temp2 = temp;
temp2.l--;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
//up
if (temp.l < L){
temp2 = temp;
temp2.l++;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
//north
if (temp.r > 1){
temp2 = temp;
temp2.r--;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
//south
if (temp.r < R){
temp2 = temp;
temp2.r++;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
//west
if (temp.c > 1){
temp2 = temp;
temp2.c--;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
//east
if (temp.c < C){
temp2 = temp;
temp2.c++;
if (vis[temp2.l][temp2.r][temp2.c] == 1){
vis[temp2.l][temp2.r][temp2.c] = 0;
q.push(temp2);
}
}
}
return -1;
}
int main(int argc, char const *argv[])
{
char temp[50];
while (scanf("%d %d %d",&L,&R,&C) && L != 0 && R != 0 && C != 0){
memset(vis,0,sizeof(vis));
for (int i = 1;i <= L;i++){
for (int j = 1;j <= R;j++){
scanf("%s",temp+1);
for (int k = 1;k <= C;k++){
if (temp[k] == '.')
vis[i][j][k] = 1;
if (temp[k] == 'S'){
vis[i][j][k] = 1;
s.l = i;
s.r = j;
s.c = k;
}else if (temp[k] == 'E'){
vis[i][j][k] = 1;
e.l = i;
e.r = j;
e.c = k;
}
}
}
}
s.dep = 0;
int ans = bfs(s,e);
if (ans >= 0)
printf("Escaped in %d minute(s).\n",ans);
else printf("Trapped!\n");
}
return 0;
}