码迷,mamicode.com
首页 > 其他好文 > 详细

POJ - 2251 - Dungeon Master (简单BFS)

时间:2015-07-25 10:42:28      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:acm   poj   bfs   

Dungeon Master
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 20450   Accepted: 7917

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找最短路径,只不过背景是三维的罢了。。


AC代码:

#include <map>
#include <set>
#include <cmath>
#include <deque>
#include <queue>
#include <stack>
#include <cstdio>
#include <cctype>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define INF 0x7fffffff
using namespace std;

int mp[35][35][35];
int vis[35][35][35];

//往六个方向走 
const int dx[] = {1, 0, 0, -1, 0, 0};
const int dy[] = {0, 1, 0, 0, -1, 0};
const int dz[] = {0, 0, 1, 0, 0, -1};

int L, R, C;

int sx, sy, sz;

int ans;

struct node {
	int x, y, z;
	int c;
	node(int _x, int _y, int _z, int _c) {
		x = _x;
		y = _y;
		z = _z;
		c = _c;
	}
};

bool bfs(int x, int y, int z) {
	memset(vis, 0, sizeof(vis));
	queue<node> que;
	que.push(node(x, y, z, 0));
	vis[x][y][z] = 1;
	while(!que.empty()) {
		node t = que.front();
		que.pop();
		for(int i = 0; i < 6; i ++) {
			int xx = t.x + dx[i];
			int yy = t.y + dy[i];
			int zz = t.z + dz[i];
			if(xx >= 0 && xx < L && yy >= 0 && yy < R && zz >= 0 && zz < C && mp[xx][yy][zz] != 1 
				&& !vis[xx][yy][zz]) {	//好吧,<L,<R,<C统统写成<=了,莫名其妙跪啦好久,傻了。。 
					if(mp[xx][yy][zz] == 4) {
						ans = t.c + 1;
						return true;
					}
					que.push(node(xx, yy, zz, t.c + 1));
					vis[xx][yy][zz] = 1;
				}
		}
	}
	return false;
}

int main() {
	while(scanf("%d %d %d", &L, &R, &C) != EOF) {
		if(L == 0 && R == 0 && C == 0) break;
		
		for(int i = 0; i < L; i ++) {
			for(int j = 0; j < R; j ++) {
				char str[55];
				scanf("%s", str);
				for(int k = 0; k < C; k ++) {
					if(str[k] == '.') {
						mp[i][j][k] = 0;
					}
					else if(str[k] == '#') {
						mp[i][j][k] = 1;
					}
					else if(str[k] == 'S') {
						mp[i][j][k] = 3;
						sx = i; sy = j; sz = k;
					}
					else if(str[k] == 'E') {
						mp[i][j][k] = 4;
					}
				}
			}
		}
		
//		for(int i = 0; i < L; i ++, printf("\n")) {
//			for(int j = 0; j < R; j++, printf("\n")) {
//				for(int k = 0; k < C; k ++) {
//					printf("%d", mp[i][j][k]);
//				}
//			}
//		}
		
		ans = 0;
		if(bfs(sx, sy, sz)) {
			printf("Escaped in %d minute(s).\n", ans);
		}
		else {
			printf("Trapped!\n");
		}

		
	}
	return 0;
}












版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ - 2251 - Dungeon Master (简单BFS)

标签:acm   poj   bfs   

原文地址:http://blog.csdn.net/u014355480/article/details/47053941

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!