• [VJ][dfs][八连通]Oil Deposits


    Oil Deposits

    Description

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots. It then analyzes each plot separately, using sensing equipment to determine whether or not the plot contains oil. A plot containing oil is called a pocket. If two pockets are adjacent, then they are part of the same oil deposit. Oil deposits can be quite large and may contain numerous pockets. Your job is to determine how many different oil deposits are contained in a grid. 

    Input

    The input file contains one or more grids. Each grid begins with a line containing m and n, the number of rows and columns in the grid, separated by a single space. If m = 0 it signals the end of the input; otherwise 1 <= m <= 100 and 1 <= n <= 100. Following this are m lines of n characters each (not counting the end-of-line characters). Each character corresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket. 

    Output

    For each grid, output the number of distinct oil deposits. Two different pockets are part of the same oil deposit if they are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets. 

    Examples

    Input

    1 1

    *

    3 5

    *@*@*

    **@**

    *@*@*

    1 8

    @@****@*

    5 5

    ****@

    *@@*@

    *@**@

    @@@*@

    @@**@

    0 0

    Output

    0

    1

    2

    2

    描述:

    找有多少个油库。

    正确解法:

    八连通,floodfill漫水填充法

    如果找到一个‘@’,ans++

    从这个点开始遍历,所有能走到的地方都变成‘*’.

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<string>
     4 #include<cstring>
     5 #include<algorithm>
     6 #include<cmath>
     7 using namespace std;
     8 int n, m;
     9 char a[110][110];
    10 bool check(int x, int y)
    11 {
    12     if (x < 1 || x > n || y<1 || y>m || a[x][y] != '@')
    13         return 0;
    14     return 1;
    15 }
    16 void dfs(int x, int y)
    17 {
    18     a[x][y] = '*';
    19     for(int dx=-1;dx<=1;dx++)
    20         for (int dy = -1; dy <= 1; dy++)
    21         {
    22             int tx = x + dx, ty = y + dy;
    23             if (check(tx, ty))
    24                 dfs(tx, ty);
    25         }
    26 }
    27 int main()
    28 {
    29     while (cin >> n >> m)
    30     {
    31         if (n == 0)    break;
    32         memset(a, 0, sizeof(a));
    33         for (int i = 1; i <= n; i++)
    34             for (int j = 1; j <= m; j++)
    35                 cin >> a[i][j];
    36         int ans = 0;
    37         for(int i=1;i<=n;i++)
    38             for(int j=1;j<=m;j++)
    39                 if (a[i][j] == '@')
    40                 {
    41                     dfs(i, j);
    42                     ans++;
    43                 }
    44         cout << ans << endl;
    45     }
    46     return 0;
    47 }
    View Code
    No matter how you feel, get up , dress up , show up ,and never give up.
  • 相关阅读:
    BUUCTF--Youngter-drive
    BUUCTF--CrackRTF
    FireShell CTF 2020 Re Simple Encryption
    2020 美国大学生数学建模论文翻译(week 2)
    仿射密码
    乘法逆元
    RC4加密与解密
    2020 BJDCTF Re encode
    2020美国大学生数学建模(MCM/ICM)A题数据及参考资料
    路由器, 美团笔试题, 差分
  • 原文地址:https://www.cnblogs.com/Kaike/p/9914559.html
Copyright © 2020-2023  润新知