uva 532 - Dungeon Master

来源:互联网 发布:python heatmap 编辑:程序博客网 时间:2024/06/02 05:31

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 Specification 

The input file consists of a number of dungeons. Each dungeon description starts with a line containing three integers LR 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 LR and C.

Output Specification 

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!
这种走迷宫的题一般就是bfs了,比较特殊点的这里是个三维迷宫,且要主要Bfs时队列数组的大小我开始开的太小了,同时注意下小细节,之前少判断一个条件在bfs里面找了好一会。不过通过这一题,再一次对bfs又熟悉不少,加油,加油!
#include<stdio.h>#include<stdlib.h>#include<string.h>char maze[35][35][35];int vis[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}};int time[35][35][35];int start_x,start_y,start_z,end_x,end_y,end_z,l,r,c;;int qx[81005],qy[81005],qz[81005];void bfs(int x,int y,int z){    int xx,yy,zz,front=0,rear=1;    qx[front]=x,qy[front]=y,qz[front]=z;    vis[x][y][z]=1;//代表访问过     while(front<rear){        for(int i=0;i<6;i++){            xx=qx[front]+dir[i][0];            yy=qy[front]+dir[i][1];            zz=qz[front]+dir[i][2];            if(xx>=0&&xx<l&&yy>=0&&yy<r&&zz>=0&&zz<c&&!vis[xx][yy][zz]&&(maze[xx][yy][zz]=='.'||maze[xx][yy][zz]=='E')){//满足要求且没被访问过                  if(xx==end_x&&yy==end_y&&zz==end_z){                     time[xx][yy][zz]=time[qx[front]][qy[front]][qz[front]]+1;                     return;                 }                 time[xx][yy][zz]=time[qx[front]][qy[front]][qz[front]]+1;                 vis[xx][yy][zz]=1;                 qx[rear]=xx,qy[rear]=yy,qz[rear]=zz,rear++;            }        }        front++;    }     }int main(){    while(scanf("%d%d%d",&l,&r,&c)==3&&(l||r||c)){         getchar();         for(int i=0;i<l;i++)             for(int j=0;j<r;j++)scanf("%s",maze[i][j]);         for(int i=0;i<l;i++)              for(int j=0;j<r;j++)                  for(int k=0;k<c;k++)                  {                     if(maze[i][j][k]=='S'){ start_x=i, start_y=j, start_z=k; }                     if(maze[i][j][k]=='E'){ end_x=i, end_y=j, end_z=k; }                  }                           memset(vis,0,sizeof(vis));         memset(time,0,sizeof(time));         bfs(start_x,start_y,start_z);         if(time[end_x][end_y][end_z])printf("Escaped in %d minute(s).\n",time[end_x][end_y][end_z]);              else  printf("Trapped!\n");    }}


原创粉丝点击