POJ2251 Dungeon Master bfs入门 TWT TokyoOlympic 1combo-2

来源:互联网 发布:51单片机蓝牙模块 编辑:程序博客网 时间:2024/06/05 15:47

Dungeon Master
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 27238 Accepted: 10679

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!

Source

Ulm Local 1997
嗯。。。。3A。。。。对于大牛来说肯定是很垃圾的纪录,但是对于我这种无限提交流来说已经挺不错了。。。

之前dfs写的比较顺手,就写了一个dfs的版本,然而T了。。。。突然想起题中要求求的是最短路,所以难道应该用BFS做?虽然如此。。。。实在太懒。。。于是改了改又叫,果然T了。。。。

老老实实地按着板子做了bfs就A了

这算明显水题吧,只不过把二维换成三维的,并没有什么困难

睡觉鸟。。。。

代码里还有我的bfs。。。

/* ━━━━━┒ ┓┏┓┏┓┃μ'sic foever!! ┛┗┛┗┛┃\○/ ┓┏┓┏┓┃ / ┛┗┛┗┛┃ノ) ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┛┗┛┗┛┃ ┓┏┓┏┓┃ ┃┃┃┃┃┃ ┻┻┻┻┻┻ */#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <cstring>#include <algorithm>#include <set>#include <map>#include <vector>#include <queue>#include <cmath>using namespace std;const int maxn=35;const int INF=8000000;struct unit{    int x;    int y;    int z;}start;int l,r,c;char save[maxn][maxn][maxn];int d[maxn][maxn][maxn];int xt,yt,zt;int move_x[6]={0,0,0,0,1,-1};int move_y[6]={0,0,-1,1,0,0};int move_z[6]={1,-1,0,0,0,0};//up down left right mae atobool flag;int result;void dfs(int x,int y,int z,int step){    //cout<<"test "<<x<<" "<<y<<" "<<z<<" "<<step<<endl;    int i;    if(save[x][y][z]=='E'){        flag=true;        result=min(result,step);        return;    }    for(i=0;i<6;i++){        if(x+move_x[i]>=1&&x+move_x[i]<=l&&y+move_y[i]>=1&&y+move_y[i]<=r&&z+move_z[i]>=1&&z+move_z[i]<=c&&save[x+move_x[i]][y+move_y[i]][z+move_z[i]]!='#'){            save[x][y][z]='#';            dfs(x+move_x[i], y+move_y[i], z+move_z[i],step+1);            save[x][y][z]='.';        }    }}int bfs(){    queue<unit> que;    int i,j,k;    for(i=1;i<=l;i++){        for(j=1;j<=r;j++){            for(k=1;k<=c;k++){                d[i][j][k]=INF;            }        }    }    que.push(start);    d[start.x][start.y][start.z]=0;    while(que.size()){        unit p=que.front();        que.pop();        if(save[p.x][p.y][p.z]=='E'){            break;        }        for(i=0;i<6;i++){            int nx=p.x+move_x[i],ny=p.y+move_y[i],nz=p.z+move_z[i];            if(1<=nx&&nx<=l&&ny>=1&&ny<=r&&nz>=1&&nz<=c&&save[nx][ny][nz]!='#'&&d[nx][ny][nz]==INF){                unit me;                me.x=nx,me.y=ny,me.z=nz;                que.push(me);                d[nx][ny][nz]=d[p.x][p.y][p.z]+1;            }        }    }    return d[xt][yt][zt];}int main(){    int i,j,k;    while(scanf("%d%d%d",&l,&r,&c)){        if(l==0||r==0||c==0){            break;        }        int x0,y0,z0;        for(i=1;i<=l;i++){            for(j=1;j<=r;j++){                for(k=1;k<=c;k++){                    cin>>save[i][j][k];                    if(save[i][j][k]=='S'){                        //x0=i,y0=j,z0=k;                        start.x=i,start.y=j,start.z=k;                    }                    if(save[i][j][k]=='E'){                        xt=i,yt=j,zt=k;                    }                }            }        }        flag=true;        //int step=0;        result=bfs();        if(result==INF){            flag=false;        }        //dfs(x0, y0, z0, step);        if(flag){            printf("Escaped in %d minute(s).\n",result);        }else{            printf("Trapped!\n");        }    }    return 0;}



0 0
原创粉丝点击