zoukankan      html  css  js  c++  java
  • Weed(BFS)

    I - Weed
    Time Limit:1000MS    Memory Limit:65536KB    64bit IO Format:%I64d & %I64u

    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 N x M 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.

    Sample Input

    sample input
    sample output
    3 3
    X..
    .X.
    .X.
    
    6
    
    sample input
    sample output
    3 4
    X..X
    .X..
    .X..
    
    12
    


    对未长草的方格用深搜超时,对已长草的方格用广搜AC

    AC CODE:

     1 //Memory: 6135 KB         Time: 218 MS
     2 //Language: GNU CPP (MinGW, GCC 4)         Result: Accepted
     3 
     4 #include <iostream>
     5 #include <string>
     6 #include <cstdio>
     7 #include <cmath>
     8 #include <cstring>
     9 #include <algorithm>
    10 #include <queue>
    11 #define LL long long
    12 #define MAXI 2147483647
    13 #define MAXL 9223372036854775807
    14 #define eps (1e-8)
    15 #define dg(i) cout << "*" << i << endl;
    16 using namespace std;
    17 
    18 struct Node
    19 {
    20     int x, y;
    21 };
    22 int n, m, ans;
    23 int a[4][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} }; //上下左右
    24 int map[1001][1001];
    25 //bool vis[1001][1001];
    26 queue<Node> que;
    27 
    28 bool in(int i, int j)
    29 {
    30     return i > -1 && i < n && j < m && j > -1;
    31 }
    32 
    33 void BFS()
    34 {
    35     while(!que.empty())
    36     {
    37         struct Node s = que.front();
    38         que.pop();
    39         for(int k = 0; k < 4; k++)
    40         {
    41             int ii = s.y + a[k][0];
    42             int jj = s.x + a[k][1];//cout << s.y <<" "<<s.x<<endl;
    43             if(in(ii, jj) && map[ii][jj] < 0)
    44             {
    45                 map[ii][jj]++;
    46                 if(!map[ii][jj])
    47                 {//dg(1)
    48                     ans++;
    49                     struct Node tmp;
    50                     tmp.y = ii;
    51                     tmp.x = jj;
    52                     que.push(tmp);
    53                 }
    54             }
    55         }
    56     }
    57 }
    58 
    59 int main()
    60 {
    61     char c;
    62     int i, j;
    63     while(scanf("%d %d", &n, &m) != EOF)
    64     {
    65         struct Node s;
    66         ans = 0;
    67         memset(map, 0, sizeof(map));
    68         //memset(vis, 0, sizeof(vis));
    69         for(i = 0; i < n; i++)
    70         {
    71             scanf("%c", &c); //吃掉空格
    72             for(j = 0; j < m; j++)
    73             {
    74                 scanf("%c", &c);
    75                 if(c == 'X')
    76                 {
    77                     map[i][j] = 0;
    78                     ans++;
    79                     s.y = i;
    80                     s.x = j;
    81                     que.push(s);
    82                 }
    83                 else map[i][j] = -2;
    84             }
    85         }
    86         BFS();
    87         printf("%d\n", ans);
    88     }
    89     return 0;
    90 }
  • 相关阅读:
    mysql增加索引、删除索引、查看索引
    微信小程序跳转页面时参数过长导致参数丢失
    微信小程序:使用wx.request()请求后台接收不到参数
    微信小程序跳转web-vie时提示appId无法读取:Cannot read property 'appId' of undefined
    tomcat正常运行一段时间后,突然访问不了项目了
    注解@Async解决异步调用问题
    Linux之acl库的安装与使用(限制Linux某用户的访问权限)
    HashMap和Hashtable的详细区别
    如何处理MySQL经常出现CPU占用率达到99%
    IntelliJ IDEA 提交代码时出现:Code analysis failed with exception: com.intellij.psi......
  • 原文地址:https://www.cnblogs.com/cszlg/p/2910474.html
Copyright © 2011-2022 走看看