POJ2251 三维的bfs

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23174   Accepted: 9030

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!



遇到的问题和思路:

       首先看到这道题感觉还好,然后那个时候还没有看题解,就单纯的用结构来写,用的是dfs,然后测试数据都是对的,就是不知道为什么一直WA。然后看了别人的解法以后,学会了如何构建三维的queue,还算是有点收获。不过本来感觉没有问题的一道题竟然WA了那么就,到现在还不知道是为什么wa。TAT


给出三维BFS的代码:AC的;


#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
#include <queue>
#include <stack>
#include <cmath>
#include <vector>
#include <cstdio>
#include <map>
#include <set>

using namespace std;

struct node{
	int x, y, z;
}res,plat,e,pl;

int ans[35][35][35];
char map1[35][35][35];
int x11, y11, z11;
int nz, nx, ny;
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};

void bfs(){
	queue <node> que;
	que.push(plat);
	while(!que.empty()){
		res = que.front();
		que.pop();
		for(int i = 0; i < 6; i++){
			nz = res.z + dz[i]; nx = res.x + dx[i]; ny = res.y + dy[i];
			if(nz >= 0 && nz < z11 && nx >= 0 && nx < x11 && ny >= 0 && ny < y11 && map1[nx][ny][nz] != '#' && ans[nx][ny][nz] == -1){
				ans[nx][ny][nz] = ans[res.x][res.y][res.z] + 1;
				pl.x = nx, pl.y = ny; pl.z = nz;
				que.push(pl);
			}
		}	
	}
}

void solve(){
	bfs();
	if(ans[e.x][e.y][e.z] == -1)printf("Trapped!\n");
	else printf("Escaped in %d minute(s).\n",ans[e.x][e.y][e.z]);
	
}

int main(){
	while(scanf("%d%d%d", &z11, &x11, &y11)!=EOF){
		if(z11 == 0 && x11 == 0 && y11 == 0)break;
		memset(map1, '\0', sizeof(map1));
		memset(ans, -1, sizeof(ans));
		for(int i = 0; i < z11; i++){
			getchar();
			for(int j = 0; j < x11; j++){
				for(int k = 0; k < y11; k++){
					scanf("%c",&map1[j][k][i]);
					if(map1[j][k][i] == 'S'){
						plat.x = j, plat.y = k, plat.z = i;
						ans[j][k][i] = 0;
					}
					if(map1[j][k][i] == 'E'){
						e.x = j, e.y = k, e.z = i;
					}
				}
				getchar();
			}
		}
		solve();
	}
	return 0;
}





 

加一个DFS的代码,不过是WA的,不知道哪里错了TAT,以后如果可以回来那就到时候再看


#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#define inf 10000000

using namespace std;

struct point{
	char plat[35][35];
}map1[111];

struct point1{
	int ans[35][35];
}res[35];

int l, r, c;
int nz, nx, ny;
int dx[]={1,-1,0,0,0,0};
int dy[]={0,0,1,-1,0,0};
int dz[]={0,0,0,0,1,-1};


void dfs(int z, int x, int y){
	for(int i = 0; i < 7; i++){
		nz = z + dz[i]; nx = x + dx[i]; ny = y + dy[i];
		if(nz >= 0 && nz < l && nx >= 0 && nx < r && ny >= 0 && ny <c && map1[nz].plat[nx][ny] != '#' && res[nz].ans[nx][ny] == -1){
		   res[nz].ans[nx][ny] = res[z].ans[x][y] + 1;
	       dfs(nz, nx, ny);
		}
	}
}


int solve(){
	for (int i = 0; i < l; i++){
		for(int j = 0; j < r; j++){
		    for(int k = 0; k < c; k++){
		    	if(map1[i].plat[j][k] == 'S'){
		    		res[i].ans[j][k] = 0;
	    			dfs(i, j, k);
	    		}
		    }
		}
	}
	for (int i = 0; i < l; i++){
		for(int j = 0; j < r; j++){
		    for(int k = 0; k < c; k++){
		    	if(map1[i].plat[j][k] == 'E'){
		    		if(res[i].ans[j][k] >= 0) printf("Escaped in %d minute(s).\n",res[i].ans[j][k]);
		    		else printf("Trapped!\n");
	    			return 0;
	    		}
		    }
		}
	}	
}


int main(){
	while(scanf("%d%d%d", &l, &r, &c)!=EOF){
		if(l == 0 && r == 0 && c == 0)break;
		memset(map1, '\0', sizeof(map1));
		memset(res, -1, sizeof(res));
		for (int i = 0; i < l; i++){
			getchar();
			for(int j = 0; j < r; j++){
				for(int k = 0; k < c; k++){
					scanf("%c",&map1[i].plat[j][k]);
				}
				getchar();
			}
		}
		solve();
	}
	return 0;
}




你可能感兴趣的:(POJ2251 三维的bfs)