kuangbin 简单搜索 B

来源:互联网 发布:sql sever外键约束 编辑:程序博客网 时间:2024/06/04 20:01

B - Dungeon Master

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.

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

Sample Output
Escaped in 11 minute(s).
Trapped!

题解:

典型的bfs三维状态搜索。
队列维护即可。

代码:

#include <iostream>#include <cstdio>#include <queue>#include <cstring>using namespace std;typedef struct P{    int z;    int x;    int y;    P(int z,int x,int y):z(z),x(x),y(y){}}P;const int INF = 0x3f3f3f3f;int L,R,C;char maze[35][35][35];int sz,sx,sy;   //定义起始点int gz,gx,gy;   //定义终点int d[35][35][35];int dir[6][3]={{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};void bfs(){    queue<P> que;    memset(d,0x3f,sizeof(d));    que.push(P(sz,sx,sy));    d[sz][sx][sy]=0;    while(que.size())    {        P p = que.front(); que.pop();        if(p.z==gz&&p.x==gx&&p.y==gy)        {            break;        }        for(int i=0;i<6;i++)        {            int nz = p.z+dir[i][0];            int nx = p.x+dir[i][1];            int ny = p.y+dir[i][2];            if(0<=nz&&nz<L&&0<=nx&&nx<R&&0<=ny&&ny<C&&maze[nz][nx][ny]!='#'&&d[nz][nx][ny]==INF)            {                que.push(P(nz,nx,ny));                d[nz][nx][ny]=d[p.z][p.x][p.y]+1;            }        }    }}int main(){    while(cin>>L>>R>>C)    {        if(L==0&&R==0&&C==0)             break;          for(int i=0;i<L;i++)             for(int j=0;j<R;j++)              for(int k=0;k<C;k++)              {                 cin>>maze[i][j][k];                 if(maze[i][j][k]=='S')                 {                     sz=i,sx=j,sy=k;                 }                 if(maze[i][j][k]=='E')                 {                     gz=i,gx=j,gy=k;                 }              }            bfs();            int ans = d[gz][gx][gy];            if(ans==INF)            {               printf("Trapped!\n");            }            else            {               printf("Escaped in %d minute(s).\n",d[gz][gx][gy]);            }    }    return 0;}
原创粉丝点击