poj 2251 Dungeon Master BFS

Dungeon Master

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 27258 Accepted: 10689

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!

 

题意:

你被关进了一个3维的地牢,给你起点和终点,求最短路径长度。
往四面走及往上下走,都花费1分钟。
本题会有多组数据,对于每一组数据,给你三个数,l, r, c。分别对应地牢的层数,长度,宽度。
S代表起点,E代表终点,#代表墙,. 代表路

分析:

这道题在大神做的poj分类中属于dfs,可是我觉得这个题用bfs比dfs更好。
用bfs就是扩展每一个节点,然后对于每一个能上或下的点进行上下扩展。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int lev, r, c, sx, sy, sc;
char map[50][50][50];
bool vis[50][50][50];
int length[300000];
int xx[10] = {1,-1, 0, 0, 0, 0};
int yy[10] = {0, 0, 0, 0,-1, 1};
int zz[10] = {0, 0,-1, 1, 0, 0};
struct qer
{
	int x, y, z;
}q[300000];
int bfs()
{
	int rear, front, dx, dy, dz;
	memset(vis, false, sizeof(vis));
    memset(length, 0, sizeof(length));
	q[0].x = sx;
	q[0].y = sy;
	q[0].z = sc;
	rear = 0;
	front = 0;
	while(front <= rear)
	{
		for(int j = 0;j < 6;j ++)
		{
			dx = q[front].x + xx[j];
            dy = q[front].y + yy[j];
            dz = q[front].z + zz[j];
            if(!vis[dx][dy][dz] && (map[dx][dy][dz] == '.' || map[dx][dy][dz] == 'E') && dx >= 0 && dx < lev && dy >= 0 && dy < r && dz >= 0 && dz < c)
			{
				vis[dx][dy][dz] = true;
				q[++rear].x = dx;
                q[rear].y = dy;
                q[rear].z = dz;
                length[rear] = length[front] + 1;
                if(map[dx][dy][dz] == 'E')
					return length[rear];
			}
		}
		front ++;
	}
	return 0;
}
int main()
{
	while(scanf("%d%d%d", &lev, &r, &c) != EOF)
	{
		if(lev == 0 && r == 0 && c == 0)
			break;
		for(int k = 0;k < lev;k ++)
			for(int i = 0;i < r;i ++)
				for(int j = 0;j < c;j ++)
				{
					cin>>map[k][i][j];
					if(map[k][i][j] == 'S')
						sx = k, sy = i, sc = j;
				}
		int ans = bfs();
		if(ans)
			printf("Escaped in %d minute(s).\n", ans);
		else
			printf("Trapped!\n");
	}
	return 0;
}

 
 
 
 
 
 
 

上一篇
下一篇