码迷,mamicode.com
首页 > 其他好文 > 详细

poj 2251(bfs)

时间:2017-08-30 20:50:29      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:space   style   using   比较   string   nbsp   搜索   pac   std   

开始做搜索专题,做的kuangbin大佬的,虽说比较老了,但基础啊。这里就是多层bfs啊

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=100;
int a[10]={1,-1,0,0,0,0};
int b[10]={0,0,1,-1,0,0};
int c[10]={0,0,0,0,1,-1};
struct note
{
    int z;
    int x;
    int y;
    int cnt;
};
char g[maxn][maxn][maxn];
int k,n,m;
int bfs(int z,int x,int y)
{   int ans=0;
      queue<note> q;
      q.push(note{z,x,y,0});
       int cc=0;
       while(q.size())
       {
           note nn=q.front();
           q.pop();
           for(int i=0;i<6;i++)
           {
               int zz=nn.z+a[i];
               int xx=nn.x+b[i];
               int yy=nn.y+c[i];
               if(zz>=1&&zz<=k&&xx>=1&&xx<=n&&yy>=1&&yy<=m&&g[zz][xx][yy]!=#)
               {
                   int mm=nn.cnt;
                   if(g[zz][xx][yy]==E) return mm+1;
                   g[zz][xx][yy]=#;
                     q.push(note{zz,xx,yy,mm+1});
               }
           }
       }
      return -1;
}
int main()
{
   while(~scanf("%d%d%d",&k,&n,&m)&&(k+n+m))
   {
           getchar();
       for(int l=1;l<=k;l++)
         for(int i=1;i<=n;i++)
              scanf("%s",g[l][i]+1);
       int sz,sx,sy;
         for(int l=1;l<=k;l++)
             for(int i=1;i<=n;i++)
              for(int j=1;j<=m;j++)
              {
                  if(g[l][i][j]==S)
                  {
                      sz=l;
                      sx=i;
                      sy=j;
                  }
              }
              g[sz][sx][sy]=#;
         int mm=bfs(sz,sx,sy);
          if(mm!=-1) printf("Escaped in %d minute(s).\n",mm);
          else printf("Trapped!\n");
   }
    return 0;
}

 

poj 2251(bfs)

标签:space   style   using   比较   string   nbsp   搜索   pac   std   

原文地址:http://www.cnblogs.com/Wangwanxiang/p/7455093.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!