zoukankan      html  css  js  c++  java
  • 41. 流感传染(宽搜)


    总时间限制: 

    1000ms

     

    内存限制: 

    65536kB

    描述

    有一批易感人群住在网格状的宿舍区内,宿舍区为n*n的矩阵,每个格点为一个房间,房间里可能住人,也可能空着。在第一天,有些房间里的人得了流感,以后每天,得流感的人会使其邻居传染上流感,(已经得病的不变),空房间不会传染。请输出第m天得流感的人数。

    输入

    第一行一个数字nn不超过100,表示有n*n的宿舍房间。
    接下来的n行,每行n个字符,’.’表示第一天该房间住着健康的人,’#’表示该房间空着,’@’表示第一天该房间住着得流感的人。
    接下来的一行是一个整数mm不超过100.

    输出

    输出第m天,得流感的人数

    样例输入

    5

    ....#

    .#.@.

    .#@..

    #....

    .....

    4

    样例输出

    16

    代码:

    #include

    using namespace std;

    const int INF=101;

    #include

    int jz[INF][INF];

    int tian[INF],d[INF*INF][2],head=0,tail=0;

    char p[INF];

    int xx[]={0,0,1,-1},n,m;

    int yy[]={1,-1,0,0};

    void input();

    void BFS();

    int main()

    {

           input();

           BFS();

           int sum=0;

           for(int i=1;i<=m;++i)

           sum+=tian[i];

           printf("%d ",sum);

           return 0;

    }

    void input()

    {

           scanf("%d",&n);

           for(int i=1;i<=n;++i)

             {

                 scanf("%s",p+1);//使用这个语句可以克服char变量读入空格与换行符的问题,p+1,表示从p[1]开始存元素

                 for(int j=1;j<=n;++j)

                 {

                        if(p[j]=='#')

                        jz[i][j]=1;

                        if(p[j]=='@')

                        {

                               jz[i][j]=-1;

                            d[++tail][0]=i;

                                d[tail][1]=j;

                                tian[1]++;

                           }

                }

             }

             scanf("%d",&m);

    }

    void BFS()

    {

           int i=1;head=0;

           int t=tail;

           while(i

           {

                  i++;

                  while(head

              {

                  ++head;

                      for(int j=0;j<4;++j)

                  {

                         int x1=d[head][0]+xx[j],y1=d[head][1]+yy[j];

                         if(x1>=1&&y1>=1&&x1<=n&&y1<=n&&jz[x1][y1]==0)

                         {

                                tian[i]++;

                                jz[x1][y1]=-1;

                                d[++tail][0]=x1;

                                d[tail][1]=y1;

                               

                         }

                        

                  }

              }

              t=tail;

          }

          

          

    }

  • 相关阅读:
    hdoj 2803 The MAX【简单规律题】
    hdoj 2579 Dating with girls(2)【三重数组标记去重】
    hdoj 1495 非常可乐【bfs隐式图】
    poj 1149 PIGS【最大流经典建图】
    poj 3281 Dining【拆点网络流】
    hdoj 3572 Task Schedule【建立超级源点超级汇点】
    hdoj 1532 Drainage Ditches【最大流模板题】
    poj 1459 Power Network【建立超级源点,超级汇点】
    hdoj 3861 The King’s Problem【强连通缩点建图&&最小路径覆盖】
    hdoj 1012 u Calculate e
  • 原文地址:https://www.cnblogs.com/csgc0131123/p/5290429.html
Copyright © 2011-2022 走看看