zoukankan      html  css  js  c++  java
  • BFS——Weed

    Weed

    Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 15   Accepted Submission(s) : 3
    Problem Description


    Andrew has visited his garden for the last time many years ago. Today's property taxes are so high, so Andrew decided to sell his garden. The land was not cultivated for a long time and now it is probably a lot of weed on it. Andrew wants to remove everything from the ground before selling. Now he wants to estimate the amount of work.

    The garden has the rectangular form and is divided into equal squares. Andrew's memory is phenomenal. He remembers which squares were occupied by the weed. For the purpose of simplicity, Andrew thinks that each square is either fully occupied by the weed or completely free from it. Andrew likes botany and he knows that if some square is free from the weed but at least two of its adjacent squares are occupied by the weed (two squares are adjacent if they have common side), that square will be also occupied by the weed soon. Andrew is pretty sure that during last years weed occupied every square possible. Please help Andrew to estimate how many squares is occupied by the weed.

    Input
    The first line of the input contains integers N and M (1 ≤ N, M ≤ 1000). Next N lines contain M characters each. Character X
    
    denotes that the corresponding square is occupied by the weed. A period character (
    

    .

    
    ) denotes an empty square.

    Output
    Print one integer denoting the number of squares occupied by the weed after so many years.

    Example(s)

    sample input

    
    

    sample output

    
    

    3 3 X.. .X. .X.

    
    

    6

    
    

    sample input

    
    

    sample output

    
    

    3 4 X..X .X.. .X..

    
    

    12 

    题意:有一个只有杂草和空地的庄园。若杂草的上下左右四个地方有超过两个地方为杂草,则该空地不久之后也会变成杂草。求庄园内杂草的数量。

    分析:用队列来放杂草,二维数组a记录该地是否为杂草,a[i][j]==0表示为杂草,空地a[x][y]初始化为-2。每一个杂草会使得它四方的a数组加1,当加到0时,空地变杂草并入队列。

    AC代码

     1 #include <cstdio>
     2 #include <queue>
     3 using namespace std;
     4 int n,m;
     5 char map[1005][1005];
     6 int a[1005][1005];
     7 int dir[4][2]={0,1,0,-1,-1,0,1,0};
     8 struct node
     9 {
    10     int x,y;
    11 };
    12 int ans;
    13 queue<node> que;
    14 int valid(int x,int y)
    15 {
    16     if(0<=x&&x<n&&0<=y&&y<m)
    17         return 1;
    18     return 0;
    19 }
    20 void bfs()
    21 {
    22     while(!que.empty())
    23     {
    24         node tmp=que.front();
    25         que.pop();
    26         for(int i=0;i<4;i++)
    27         {
    28             int fx=tmp.x+dir[i][0];
    29             int fy=tmp.y+dir[i][1];
    30             if(!valid(fx,fy))
    31                 continue;
    32             if(a[fx][fy]==0)
    33                 continue;
    34             a[fx][fy]++;
    35             if(a[fx][fy]==0)
    36             {
    37                 ans++;
    38                 que.push(node{fx,fy});
    39             }
    40         }
    41     }
    42     printf("%d
    ",ans);
    43 }
    44 int main()
    45 {
    46     while(~scanf("%d%d",&n,&m))
    47     {
    48         ans=0;
    49         while(!que.empty())
    50             que.pop();
    51         for(int i=0;i<n;i++)
    52             scanf("%s",map[i]);
    53         for(int i=0;i<n;i++)
    54             for(int j=0;j<m;j++)
    55         {
    56             if(map[i][j]=='X')
    57             {
    58                  a[i][j]=0;
    59                  ans++;
    60                  que.push(node{i,j});
    61             }
    62             else
    63                 a[i][j]=-2;
    64 
    65         }
    66         bfs();
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    自己定义Actionbar
    创建自己的Repo Server
    AI案例
    贝叶斯定理,从白袜到飞机失事再到人工智能
    jupyter notebook 安装代码提示功能
    Jupyter notebook 自动补全
    27 个Jupyter Notebook的小提示与技巧
    Windows下的Jupyter Notebook 安装与自定义启动
    以太坊联盟链 parity 节点搭建
    ubuntu上面Parity 安装
  • 原文地址:https://www.cnblogs.com/onlyli/p/7207121.html
Copyright © 2011-2022 走看看