poj2251——Dungeon Master

来源:互联网 发布:淘宝商品排名 编辑:程序博客网 时间:2024/06/05 15:29

题目大意:在三维地牢中移动可以前后左右,也可以上下楼,一共六个方向,走一步需要一分钟,问从地牢中逃出最少需要几分钟

输入:(可以有几个case 以0 0 0结束输入)

            地牢层数L  每层地牢的行数R 列数C(所有LRC都不超过30)

            第i行的地牢情况(#表示墙 .表示空地 S表示入口 E表示出口)

输出:如果可以走出则输出为Escaped in x minute(s).

           如果走不出则输出为Trapped!

分析:bfs求最短路。与二维类似,只不过这道题是三维,四个方向改成六个方向即可。

           map[][][]:表示第k层地牢的第r行第c列

           输入的同时记录入口S位置和出口E位置

           bfs六个方向,判断能不能走,如果越界或撞墙或已经遍历过,就continue下一个方向,否则意味着能走,将该点入队

代码:转载自http://blog.csdn.net/libin56842/article/details/23702395

  1. #include <iostream>  
  2. #include <stdio.h>  
  3. #include <string.h>  
  4. #include <queue>  
  5. #include <algorithm>  
  6. using namespace std;  
  7.   
  8. char map[35][35][35];  
  9. int vis[35][35][35];  
  10. int k,n,m,sx,sy,sz,ex,ey,ez;  
  11. int to[6][3] = {{0,0,1},{0,0,-1},{0,1,0},{0,-1,0},{1,0,0},{-1,0,0}};  
  12.   
  13. struct node  
  14. {  
  15.     int x,y,z,step;  
  16. };  
  17.   
  18. int check(int x,int y,int z)  
  19. {  
  20.     if(x<0 || y<0 || z<0 || x>=k || y>=n || z>=m)  
  21.         return 1;  
  22.     else if(map[x][y][z] == '#')  
  23.         return 1;  
  24.     else if(vis[x][y][z])  
  25.         return 1;  
  26.     return 0;  
  27. }  
  28.   
  29. int bfs()  
  30. {  
  31.     int i;  
  32.     node a,next;  
  33.     queue<node> Q;  
  34.     a.x = sx,a.y = sy,a.z = sz;  
  35.     a.step = 0;  
  36.     vis[sx][sy][sz] = 1;  
  37.     Q.push(a);  
  38.     while(!Q.empty())  
  39.     {  
  40.         a = Q.front();  
  41.         Q.pop();  
  42.         if(a.x == ex && a.y == ey && a.z == ez)  
  43.             return a.step;  
  44.         for(i = 0; i<6; i++)  
  45.         {  
  46.             next = a;  
  47.             next.x = a.x+to[i][0];  
  48.             next.y = a.y+to[i][1];  
  49.             next.z = a.z+to[i][2];  
  50.             if(check(next.x,next.y,next.z))  
  51.                 continue;  
  52.             vis[next.x][next.y][next.z] = 1;  
  53.             next.step = a.step+1;  
  54.             Q.push(next);  
  55.         }  
  56.     }  
  57.     return 0;  
  58. }  
  59.   
  60. int main()  
  61. {  
  62.     int i,j,r;  
  63.     while(scanf("%d%d%d",&k,&n,&m),n+m+k)  
  64.     {  
  65.         for(i = 0; i<k; i++)  
  66.         {  
  67.             for(j = 0; j<n; j++)  
  68.             {  
  69.                 scanf("%s",map[i][j]);  
  70.                 for(r = 0; r<m; r++)  
  71.                 {  
  72.                     if(map[i][j][r] == 'S')  
  73.                     {  
  74.                         sx = i,sy = j,sz = r;  
  75.                     }  
  76.                     else if(map[i][j][r] == 'E')  
  77.                     {  
  78.                         ex = i,ey = j,ez = r;  
  79.                     }  
  80.                 }  
  81.             }  
  82.         }  
  83.         memset(vis,0,sizeof(vis));  
  84.         int ans;  
  85.         ans = bfs();  
  86.         if(ans)  
  87.             printf("Escaped in %d minute(s).\n",ans);  
  88.         else  
  89.             printf("Trapped!\n");  
  90.     }  
  91.   
  92.     return 0;  
  93. }