zoj 1940 模拟三维bfs

来源:互联网 发布:python lxml etree 编辑:程序博客网 时间:2024/06/04 19:43

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!

题解:题目很吓人,但其实是个水题,也是裸的bfs

#include <iostream>#include <cstdio>#include <algorithm>#include <queue>using namespace std;int l,r,c;char mp[40][40][40];int sx,sy,sz,ex,ey,ez;int disx[6]={1,-1,0,0,0,0};int disy[6]={0,0,0,0,-1,1};int disz[6]={0,0,1,-1,0,0};const int inf=9999999;int ans;struct node {    int x,y,z;    int step;};int check(int x,int y,int z) {    if(x<0||y<0||z<0||x>=l||y>=r||z>=c) return 1;    if(mp[x][y][z]=='#') return 1;    return 0;}void bfs() {    queue<node> q;    node a,next;    a.x=sx;    a.y=sy;    a.z=sz;    a.step=0;    q.push(a);    mp[sx][sy][sz]='#';    while(!q.empty()) {        a=q.front();q.pop();        if(a.x==ex&&a.y==ey&&a.z==ez)            if(a.step<=ans) ans=a.step;        for(int i=0;i<6;i++) {            next.x=a.x+disx[i];            next.y=a.y+disy[i];            next.z=a.z+disz[i];            next.step=a.step+1;            if(check(next.x,next.y,next.z)) continue;            mp[next.x][next.y][next.z]='#';            q.push(next);        }    }}int main() {//  freopen("input.txt","r",stdin);    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++) {                scanf("%s",mp[i][j]);            }        }        for(int i=0;i<l;i++) {            for(int j=0;j<r;j++) {                for(int x=0;x<c;x++) {                    if(mp[i][j][x]=='S') {                        sx=i;                        sy=j;                        sz=x;                    }                    if(mp[i][j][x]=='E') {                        ex=i;                        ey=j;                        ez=x;                    }                }            }        }        ans=inf;        bfs();        if(ans==inf) printf("Trapped!\n");        else printf("Escaped in %d minute(s).\n",ans);    }    return 0;}
原创粉丝点击