POJ 2251 Dungeon Master

来源:互联网 发布:社交软件营销策略 编辑:程序博客网 时间:2024/06/07 16:12

这道题就是给你一个三维的坐标求S到E的最短路,如果有就输出距离没有就输出Trapped!。

就是一个BFS寻找最短路径、、和HDU的1429胜利大逃亡基本上一样、、

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 14210 Accepted: 5519

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 5S.....###..##..###.#############.####...###########.#######E1 3 3S###E####0 0 0

Sample Output

Escaped in 11 minute(s).Trapped!
#include <stdio.h>#include <string.h>#include <queue>#include <stdlib.h>#include <iostream>using namespace std;int map[35][35][35], sum;int v[35][35][35];int  n, m, t;int x2, y2, z2, x1, y1, z1;int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};struct node{    int x, y, z;    int step;} f[100005];int charg(int a, int b, int c){    if(a>=0 && a<n && b>=0 && b<m && c>=0 && c<t)        return 1;    else        return 0;}int bfs(){    struct node p;    int l = 0, r = 0;    f[l].x = x1;    f[l].y = y1;    f[l].z = z1;    f[l].step = 0;    v[x1][y1][z1] = 1;    r++;    while(l < r)    {        for(int i = 0; i < 6; i++)        {            p.x = f[l].x+to[i][0], p.y = f[l].y+to[i][1], p.z = f[l].z+to[i][2];            if(charg(p.x, p.y, p.z))                if(map[p.x][p.y][p.z] && !v[p.x][p.y][p.z])                {                    v[p.x][p.y][p.z] = 1;                    p.step = f[l].step+1;                    if(p.x == x2 && p.y == y2 && p.z == z2)                    {                        sum = p.step;                        return 1;                    }                    else                        f[r++] = p;                }        }        l++;    }    return 0;}int main(){    int i, j, k;    char s[101];    while(~scanf("%d %d %d", &n, &m, &t) &&(n||m||t))    {        memset(v , 0 , sizeof(v));        for(i = 0; i < n; i++)            for(j = 0; j < m; j++)            {                scanf("%s",s);                for(k = 0; k < t; k++)                {                    if(s[k] == 'S')                    {                        x1 = i, y1 = j, z1 = k;                        map[i][j][k] = 1;                    }                    else if(s[k] == 'E')                    {                        x2 = i, y2 = j, z2 = k;                        map[i][j][k] = 1;                    }                    else if(s[k] == '#')                        map[i][j][k] = 0;                    else if(s[k] == '.')                        map[i][j][k] = 1;                }            }        if(bfs())        printf("Escaped in %d minute(s).\n",sum);        else        printf("Trapped!\n");    }    return 0;}


原创粉丝点击