程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ 2251 Dungeon Master

POJ 2251 Dungeon Master

編輯:C++入門知識

Dungeon Master Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12655 Accepted: 4912 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 5 S.... .###. .##.. ###.#   ##### ##### ##.## ##...   ##### ##### #.### ####E   1 3 3 S## #E# ###   0 0 0 Sample Output   Escaped in 11 minute(s). Trapped! Source   Ulm Local 1997 考察點: bfs搜索 [cpp] view plaincopy #include <stdio.h>   #include <string.h>   #include <math.h>   int a[50][50][50],status[50][50][50],sum[50][50][50];   int stax,stay,staz,endy,endx,endz,t,n,m;   int vex[]={0,0,0,0,1,-1};   int vey[]={0,0,-1,1,0,0};   int vez[]={-1,1,0,0,0,0};   char s1[100];   struct num   {       int x,y,z;   }queue[1000000];   int main()   {       int bfs();       int i,j,s,k,x;       char c;       while(scanf("%d %d %d%*c",&t,&n,&m)!=EOF)       {           if(!n&&!m&&!t)           {               break;           }           for(i=0;i<=t-1;i++)           {               for(j=0;j<=n-1;j++)               {                   for(x=0;x<=m-1;x++)                   {                       scanf("%c",&c);                       if(c=='#')                       {                           a[i][j][x]=0;                       }else if(c=='.')                       {                           a[i][j][x]=1;                       }else if(c=='S')                       {                           a[i][j][x]=1;                           staz=i; stax=j; stay=x;                       }else if(c=='E')                       {                           a[i][j][x]=1;                           endz=i; endx=j; endy=x;                       }                   }                   getchar();               }               gets(s1);           }           k=bfs();           if(k!=-1)           {               printf("Escaped in %d minute(s).\n",k);           }else           {               printf("Trapped!\n");           }       }       return 0;   }   int bfs()   {       int i,j,base,top,x,y,z,k,xend,yend,zend;       base=top=0;       queue[top].x=stax; queue[top].y=stay; queue[top++].z=staz;       memset(status,0,sizeof(status));       status[stax][stay][staz]=1;       memset(sum,0,sizeof(sum));       k=0;       while(base<top)       {           x=queue[base].x; y=queue[base].y; z=queue[base++].z;           if(x==endx&&y==endy&&z==endz)           {               k=1;               break;           }           for(i=0;i<=5;i++)           {  www.2cto.comwww.2cto.com             xend=x+vex[i]; yend=y+vey[i]; zend=z+vez[i];               if(xend>=0&&xend<=n-1&¥d>=0&¥d<=m-1&&zend>=0&&zend<=t-1&&!status[xend][yend][zend]&&a[zend][xend][yend])               {                   queue[top].x=xend; queue[top].y=yend; queue[top++].z=zend;                   sum[xend][yend][zend]=sum[x][y][z]+1;                   status[xend][yend][zend]=1;               }           }       }       if(k)       {           return (sum[endx][endy][endz]);       }else       {           return -1;       }   }    

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved