Dungeon Master
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 76082 Accepted: 27287
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
Ulm Local 1997
三维——求到达目的地最少步数
没一开始想到 bfs 不应该,不应该…
dfs版本(超时)
#include<iostream>
#include<memory.h>
using namespace std;
int ans, end_l, end_r, end_c;
int l, r, c;
bool dun[35][35][35];
int dp[35][35][35];
int L[8] = {0, 0, 0, 0, -1, 1};
int R[8] = {0, 0, -1, 1, 0, 0};
int C[8] = {-1, 1, 0, 0, 0, 0};
bool judge(int x, int y, int z){
if(x<=0||x>l||y<=0||y>r||z<=0||z>c) return false;
return dun[x][y][z];
}
void f(int x, int y, int z, int num){
if(dp[x][y][z]<=num) return;
dp[x][y][z] = num;
if(x==end_l&&y==end_r&&z==end_c){
ans = min(ans, num);
return;
}
for(int i=0; i<6; i++){
int dl = x+L[i];
int dr = y+R[i];
int dc = z+C[i];
if(judge(dl, dr, dc)){
dun[dl][dr][dc] = false;
f(dl, dr, dc, num+1);
dun[dl][dr][dc] = true;
}
}
return;
}
int main(){
while(1){
scanf("%d%d%d", &l, &r, &c);
ans = 1e9;
memset(dun, false, sizeof(dun));
if(l==0&&r==0&&c==0) break;
int start_l, start_r, start_c;
for(int i=1; i<=l; i++)
for(int j=1; j<=r; j++)
for(int k=1; k<=c; k++){
char ch;
dp[i][j][k] = 1e9;
cin>>ch;
if(ch == 'S'){
start_l = i;
start_r = j;
start_c = k;
}
else if(ch == 'E'){
end_l = i;
end_r = j;
end_c = k;
dun[i][j][k] = true;
}
else if(ch == '.')
dun[i][j][k] = true;
}
f(start_l, start_r, start_c, 0);
if(ans!=1e9)
cout<<"Escaped in "<<ans<<" minute(s).\n";
else
cout<<"Trapped!\n";
}
return 0;
}
bfs 版本(AC)
#include<iostream>
#include<memory.h>
#include<queue>
using namespace std;
int ans, end_l, end_r, end_c;
int l, r, c;
bool dun[35][35][35];
int L[8] = {0, 0, 0, 0, -1, 1};
int R[8] = {0, 0, -1, 1, 0, 0};
int C[8] = {-1, 1, 0, 0, 0, 0};
struct node{
int x, y, z;
int time;
}Node, top;
bool judge(int x, int y, int z){
if(x<=0||x>l||y<=0||y>r||z<=0||z>c)
return false;
return dun[x][y][z];
}
void bfs(int x, int y, int z){
Node.time = 0;
Node.x = x;
Node.y = y;
Node.z = z;
queue<node> q;
q.push(Node);
while(!q.empty()){
top = q.front();
q.pop();
if(top.x==end_l&&top.y==end_r&&top.z==end_c){
ans = min(ans, top.time);
continue;
}
for(int i=0; i<6; i++){
int dl = top.x+L[i];
int dr = top.y+R[i];
int dc = top.z+C[i];
if(judge(dl, dr, dc)){
dun[dl][dr][dc] = false;
Node.x = dl;
Node.y = dr;
Node.z = dc;
Node.time = top.time+1;
q.push(Node);
}
}
}
return;
}
int main(){
while(cin>>l>>r>>c){
ans = 1e9;
memset(dun, false, sizeof(dun));
if(l==0&&r==0&&c==0) break;
int start_l, start_r, start_c;
for(int i=1; i<=l; i++)
for(int j=1; j<=r; j++)
for(int k=1; k<=c; k++){
char ch;
cin>>ch;
if(ch == 'S'){
start_l = i;
start_r = j;
start_c = k;
}
else if(ch == 'E'){
end_l = i;
end_r = j;
end_c = k;
dun[i][j][k] = true;
}
else if(ch == '.')
dun[i][j][k] = true;
}
bfs(start_l, start_r, start_c);
if(ans!=1e9)
cout<<"Escaped in "<<ans<<" minute(s).\n";
else
cout<<"Trapped!\n";
}
return 0;
}
总结
dfs ----- 注重过程
bfs ----- 注重结果