POJ 2251:dungeon Master "BFS"

Source: Internet
Author: User

Dungeon Mastertime limit:2000/1000ms (java/other) Memory limit:131072/65536k (Java/other) total submission (s): 9 A ccepted Submission (s): 7Problem descriptionyou is trapped in a 3D dungeon and need to find the quickest a- The dungeon is composed of a unit cubes which may or may isn't being 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 are surrounded by solid rock in all sides.

is an escape possible? If Yes, how long would it take?

Inputthe input consists of a number of dungeons. Each of the dungeon description starts with a line containing three integers L, R and C (all limited to the size).
L is the number of levels making up the dungeon.
R and C is the number of rows and columns making up the plan of each level.
Then there'll follow L blocks of R lines each containing C characters. Each character describes one cell of the dungeon. A cell full of rock was indicated by a ' # ' and empty cells were 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.
Outputeach 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 isn't possible to escape, print the line

trapped!

Ac-code:

#include <cstdio> #include <cstring> #include <queue> #define INF 0xffffffint ans,l,r,c,ex,ey,ez,vis[ 35][35][35];char a[35][35][35];int dx[6]={0,0,0,0,1,-1};int dy[6]={1,-1,0,0,0,0};int dz[6]={0,0,1,-1,0,0};using namespace std;struct Node {int x,y,z,step;friend bool operator < (node A,node b) {return a.step>b.step;}} temp,p;void BFS (int x1,int y1,int z1) {P.x=x1;p.y=y1;p.z=z1;p.step=0;vis[p.x][p.y][p.z]=1;priority_queue<node >q;q.push (P); while (!q.empty ()) {p=q.top (), Q.pop (); for (int i=0;i<6;i++) {temp.x=p.x+dx[i];temp.y=p.y+dy[i]; Temp.z=p.z+dz[i];temp.step=p.step+1;if (!vis[temp.x][temp.y][temp.z]&&temp.x>=0&&temp.x<l &AMP;&AMP;TEMP.Y&GT;=0&AMP;&AMP;TEMP.Y&LT;R&AMP;&AMP;TEMP.Z&GT;=0&AMP;&AMP;TEMP.Z&LT;C) {if (temp.x==ex& &temp.y==ey&&temp.z==ez) {printf ("escaped in%d minute (s). \ n", temp.step); return;} Vis[temp.x][temp.y][temp.z]=1;q.push (temp);}}} printf ("trapped!\n");} int main () {int i,j,k,x,y,z;while (scanf ("%d%d%d",&l,&r,&c), l| | r| | c) {memset (vis,0,sizeof (VIS)), for (I=0;i<l;i++,getchar ())} {for (j=0;j<r;j++) {GetChar (), for (k=0;k<c;k++) { scanf ("%c", &a[i][j][k]); if (a[i][j][k]== ' S ') {x=i;y=j;z=k;} else if (a[i][j][k]== ' E ') {ex=i;ey=j;ez=k;} else if (a[i][j][k]== ' # ') Vis[i][j][k]=1;elsevis[i][j][k]=0;}}} Ans=inf;bfs (x, y, z);} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2251:dungeon Master "BFS"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.