poj 2251

    最短路径一定要用BFS,
DFS求最短路径不及BFS系列


点击(此处)折叠或打开

  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <cstring>
  4. #include <queue>

  5. using namespace std;

  6. #define MAX_V (40+1)
  7. struct point{
  8.     int x;
  9.     int y;
  10.     int z;
  11.     int l;
  12. };

  13. char T[MAX_V][MAX_V][MAX_V];
  14. char V[MAX_V][MAX_V][MAX_V];

  15. int l,r,c;
  16. int si,sj,sk;

  17. struct move{
  18.     int x;
  19.     int y;
  20.     int z;
  21. }MOVE[]={ {-1,0,0},{1,0,0},{0,-1,0},{0,1,0},{0,0,-1},{0,0,1} };

  22. int ml = sizeof(MOVE) /sizeof(move);

  23. int bfs()
  24. {
  25.     
  26.     struct point Q[50000];
  27.     Q[0].x=si;
  28.     Q[0].y=sj;
  29.     Q[0].z=sk;
  30.     Q[0].l=1;    
  31.     int h=0,t=1;

  32.     memset(V,0,sizeof(V));
  33.     V[si][sj][sk]=1;

  34.     while(h<t)
  35.     {
  36.         //point p=Q.front();
  37.         //Q.pop();
  38.         point p=Q[h];
  39.         h++;
  40.         //printf("(%d,%d,%d)=%d\n",p.x,p.y,p.z,p.l);
  41.         //V[x][y][z]=1; //习惯性的再这里加访问标志
            //如果不是求最小生成树等BFS变种,其实一个元素放到队列里,就是已经访问了。
  42.         for(int m=0;m<ml;m++)
  43.         {
  44.             int x=MOVE[m].x + p.x;
  45.             int y=MOVE[m].y + p.y;
  46.             int z=MOVE[m].z + p.z;

  47.             if( x < 1 || y < 1 || z < 1)
  48.                 continue;

  49.             if(x > l || y > r || z > c)
  50.                 continue;

  51.             if(V[x][y][z] != 0)
  52.                 continue;

  53.             if(T[x][y][z] == 'E')
  54.                 return p.l;    

  55.             if(T[x][y][z] == '.')
  56.             {
  57.                 Q[t].x=x;
  58.                 Q[t].y=y;
  59.                 Q[t].z=z;
  60.                 Q[t].l=p.l+1;    
  61.                 t++;
  62.                 V[x][y][z]=1;  //所以BFS的visit应该放在这里设标志。这样可以减少很多出队入队操作,也避免重复
  63.             }
  64.         }
  65.         
  66.     }

  67.     return -1;
  68. }
  69. int main()
  70. {
  71.     while(cin>>l>>r>>c)
  72.     {
  73.         if(l <= 0)
  74.             return 0;
  75.         for(int i=1;i<=l;i++)
  76.         {
  77.             for(int j=1;j<=r;j++)
  78.             {
  79.                 for(int k=1;k<=c;k++)
  80.                 {
  81.                     char c;
  82.                     cin>>c;
  83.                     T[i][j][k]=c;
  84.                     //printf("read (%d,%d,%d)=%c\n",i,j,k,(char)T[i][j][k]);
  85.                     if( T[i][j][k] == 'S')
  86.                     {
  87.                         si=i;
  88.                         sj=j;
  89.                         sk=k;
  90.                     }
  91.                 }
  92.             }
  93.         }

  94.         int ret = bfs();
  95.         if(ret < 0)
  96.             cout<<"Trapped!"<<endl;
  97.         else
  98.             cout<<"Escaped in "<<ret<<" minute(s)."<<endl;
  99.     }
  100. }


<script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=3&lang=zh"></script> <script type=text/javascript charset=utf-8 src="http://static.bshare.cn/b/bshareC0.js"></script>
阅读(13) | 评论(0) | 转发(0) |
0

上一篇:poj 3083

下一篇:算法导论 DP 矩阵链乘法

给主人留下些什么吧!~~
评论热议
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值