zoukankan      html  css  js  c++  java
  • 生化武器2--bfs

      1 #include<cstdio>   //bfs;
      2 #include<iostream>
      3 #include<queue>
      4 #include<memory.h>
      5 using namespace std;
      6 struct data
      7 {
      8     int x,y,t;
      9 }now,pos;
     10 int n,m,t,dis[4][2]={0,1,0,-1,1,0,-1,0};
     11 char map[105][105];
     12 void read()
     13 {
     14     memset(map,'',sizeof(map));
     15     cin>>n>>m>>t;
     16     for(int i=0;i<n;i++)
     17     {
     18         for(int j=0;j<m;j++)
     19         {
     20             cin>>map[i][j];
     21             if(map[i][j]=='G')//找出图中G的位置,位置存在now中 。
     22                 now.x=i,now.y=j;
     23             else if(map[i][j]=='S')//找出图中S的位置,位置存在pos中 。
     24                 pos.x=i,pos.y=j;
     25         }
     26     }
     27 }
     28 bool cherk()
     29 {
     30     for(int i=0;i<n;i++)
     31     {
     32         for(int j=0;j<m;j++)
     33             if(map[i][j]=='S')//图中存在可走的安全区域S
     34                 return 1;
     35     }
     36     return 0;
     37 }
     38 bool bfs()
     39 {
     40     queue<data>q;
     41     queue<data>s;
     42     q.push(now);
     43     s.push(pos);
     44     while(t--)
     45     {
     46         data next;
     47         int G=q.size();//相当于每层结点的个数
     48         while(G--)//取该层的所有结点,即同一时刻所扩散的位置
     49         {
     50             now=q.front();
     51             q.pop();
     52             for(int j=0;j<4;j++)
     53             {
     54                 next.x=now.x+dis[j][0];
     55                 next.y=now.y+dis[j][1];
     56                 if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && (map[next.x][next.y]=='.' || map[next.x][next.y]=='S'))
     57                 {
     58                     map[next.x][next.y]='G';
     59                     q.push(next);    //入栈;
     60                 }
     61             }
     62         }
     63         if(q.size()==0)//如果走不动了,直接break
     64             break;
     65         G=s.size();
     66         while(G--)
     67         {
     68             now=s.front();
     69             s.pop();
     70             if(map[now.x][now.y]=='G')//若now已经有毒气,在该格是人已死,不用再考虑。
     71                 continue;
     72             for(int j=0;j<4;j++)
     73             {
     74                 next.x=now.x+dis[j][0];
     75                 next.y=now.y+dis[j][1];
     76                 if(next.x>=0 && next.x<n && next.y>=0 && next.y<m && map[next.x][next.y]=='.' )
     77                 {
     78                     map[next.x][next.y]='S';
     79                     s.push(next);
     80                 }
     81             }
     82         }
     83     }
     84     return cherk();
     85 }
     86 void print()
     87 {
     88     for(int i=0;i<n;i++)
     89     {
     90         for(int j=0;j<m;j++)
     91             cout<<map[i][j];
     92         cout<<endl;
     93     }
     94 }
     95 void solve()
     96 {
     97     if(bfs())    //能够成功逃脱;
     98         print();
     99     else
    100         cout<<"No"<<endl;
    101 }
    102 int main()
    103 {
    104     int cas;
    105     cin>>cas;
    106     while(cas--)
    107     {
    108         read();
    109         solve();
    110     }
    111     return 0;
    112 }
    简单的bfs的题目
  • 相关阅读:
    再谈后兵 FileInfo 与 DirectoryInfo类 清晰的winform界面(技术改变世界cnblog)
    在.NET Core中使用Channel(二)
    C#中的依赖注入和IoC容器
    改进你的c#代码的5个技巧(一)
    将WCF迁移到gRPC
    在.NET Core中使用Channel(三)
    在.NET Core中使用Channel(一)
    .NET 5 源代码生成器——MediatR——CQRS
    在大型软件项目中如何处理错误和异常
    在 ASP.NET Core和Worker Service中使用Quartz.Net
  • 原文地址:https://www.cnblogs.com/wm1995/p/3757983.html
Copyright © 2011-2022 走看看