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.
  • 相关阅读:
    leetcode 之Jump Game
    leetcode 之 Symmetric Tree
    leetcode 之 House Robber
    设计模式之建造者模式
    centos7 yum tab 补全
    设计模式之适配器模式
    设计模式之状态模式
    设计模式之外观模式
    设计模式之模板方法模式
    对以<uses-permission android:maxSdkVersion="xx" /> 中的说明
  • 原文地址:https://www.cnblogs.com/Kaike/p/9914559.html
Copyright © 2011-2022 走看看