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.
  • 相关阅读:
    appium-flutter-driver 测试flutter_boost项目_Java语言
    appium-flutter-driver 测试flutter项目
    Android Studio 制作出来的9图无法进行拖拉,导致无法制作出正确的9图
    Flutter 正确删除emoji表情/正确分割字符串
    如何清除git仓库的所有提交记录,成为一个新的干净仓库
    js中this的总结
    python判断时间是否在某个时间段里面的三种方式
    centos7使用rpm包安装mysql5.74
    django学习之 从服务端上传文档与下载文档接口
    python 之阿里云短信服务接入流程短信接口
  • 原文地址:https://www.cnblogs.com/Kaike/p/9914559.html
Copyright © 2011-2022 走看看