zoukankan      html  css  js  c++  java
  • [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.
  • 相关阅读:
    17.Letter Combinations of a Phone Number(递归生成序列)
    BZOJ 4052 Magical GCD
    管理学的入门经典书籍,初级管理学书籍推荐
    关于经营战略的书,商业战略书籍推荐
    如何成为一名合格的销售管理者?
    能帮你提升沟通能力的书籍推荐
    提升领导力心得体会
    口才和说服力的书籍推荐,学会说服别人你需要看这本书
    团队管理的书籍推荐 ,这些书教你如何打造团队
    管理学必读书单推荐:这些书教你学好经营管理知识
  • 原文地址:https://www.cnblogs.com/Kaike/p/9914559.html
Copyright © 2011-2022 走看看