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

Posted kaike

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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

 

以上是关于[VJ][dfs][八连通]Oil Deposits的主要内容,如果未能解决你的问题,请参考以下文章

油田(Oil Deposits)-用DFS求连通块

UVa 572 Oil Deposits (Floodfill && DFS)

题目1460:Oil Deposit

Oil Deposit

九度oj 题目1460:Oil Deposit

DFS求水洼的数目