POJ

来源:互联网 发布:金薇内衣网络传销吗 编辑:程序博客网 时间:2024/04/30 03:12

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 37134 Accepted: 14185

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!

题意:有一个高为l, 长为r, 宽为c,的三维迷宫,‘.’为空间,‘#’为石头,‘S’ 为起点,‘E’为重点,求起点到重点的最短距离。

解决:虽说是三维,其实都一样,BFS入门。

细节:

#include <iostream>#include <cstdio>#include <string>#include <cstring>#include <algorithm>#include <queue>#define fin freopen("in.txt", "r", stdin)#define line printf("\n-----------------\n")#define rearr(a,n) for(int i=0;i<n;i++)scanf("%d",&a[i]);using namespace std;const long long mod = 1000000007;const int maxn = 30+ 5;int l, r, c;char ss[maxn][maxn][maxn];int ans;int sl, sr, sc;int dir[6][3] = {1,0,0,-1,0,0,0,1,0,0,-1,0,0,0,1,0,0,-1};struct Point{    int l, x, y, cnt;}s, e;queue<Point> q;int bfs(int l, int x, int y){    while(!q.empty()) q.pop();    s.l = l, s.x = x, s.y = y, s.cnt = 0;    ss[l][x][y] = '#';    q.push(s);    while(!q.empty()){        s = q.front(); q.pop();//        if(s[u.l][u.x][u.y] == 'E'){return ans;}        for(int i = 0; i < 6; i++){            int ll = s.l + dir[i][0];            int xx = s.x + dir[i][1];            int yy = s.y + dir[i][2];            if(ss[ll][xx][yy] == '#') continue;            if(ss[ll][xx][yy] == 'E') return s.cnt+1;            ss[ll][xx][yy] = '#';            e.l = ll, e.x = xx, e.y = yy, e.cnt = s.cnt + 1;            q.push(e);        }    }    return -1;}int main(){    while(cin >> l >> r >> c && l && r && c){        memset(ss, '#', sizeof(ss));        for(int i = 1; i <= l; i++){            for(int j = 1; j <= r; j++){                scanf("%s", ss[i][j]+1);                for(int k = 1; k <= c; k++){                    if(ss[i][j][k] == 'S') sl = i, sr = j, sc = k;                }            }        }        ans = bfs(sl, sr, sc);        if(ans == -1) printf("Trapped!\n");        else printf("Escaped in %d minute(s).\n", ans);    }}


原创粉丝点击