UVa 572 Oil Deposits(DFS)

 Oil Deposits 

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  and .
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.

Sample Input

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0

Sample Output

0
1
2
2

题意  计算@连通块的数量  典型的dfs应用

#include<cstdio>
#include<cstring>
using namespace std;
#define r i+x[k]
#define c j+y[k]
const int N = 105;
char mat[N][N];
int n, x[8] = { -1, -1, -1, 0, 0, 1, 1, 1};
int m, y[8] = { -1, 0, 1, -1, 1, -1, 0, 1};

int dfs (int i, int j)
{
    if (mat[i][j] == '*') return 0;
    mat[i][j] = '*';
    for (int k = 0; k < 8; ++k)
        if (r > 0 && r <= n && c > 0 && c <= m && mat[r][c] == '@')
            dfs (r, c);
    return 1;
}

int main()
{
    while (scanf ("%d%d", &n, &m), n)
    {
        int ans = 0;
        for (int i = 1; i <= n; ++i)
            scanf ("%s", mat[i] + 1);
        for (int i = 1; i <= n; ++i)
            for (int j = 1; j <= m; ++j)
                ans += dfs (i, j);
        printf ("%d\n", ans);
    }
    return 0;
}

UVa 572 Oil Deposits(DFS),布布扣,bubuko.com

时间: 2024-08-24 08:33:40

UVa 572 Oil Deposits(DFS)的相关文章

UVa 572 Oil Deposits(DFS求8连通块)

题意  求n*m矩阵中'@'连通块的个数  两个'@'在一个九宫格内就属于一个连通块 最基础的DFS  遇到@就递归扫描周围8个并标记当前格子已访问  然后就得到答案了 #include<cstdio> using namespace std; const int N = 110; char mat[N][N]; int dfs(int r, int c) { if(mat[r][c] != '@') return 0; else { mat[r][c] = '*'; for(int i =

UVA - 572 - Oil Deposits (图的DFS!)

UVA - 572 Oil Deposits Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description  Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works

UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常量数组或者写8条DFS调用. 下述算法是:种子填充(floodfill) 两种连通区域 四连通区域:从区域内一点出发,可通过上.下.左.右四个方向的移动组合,在不越出区域的前提下,能到达区域内的任意像素 八连通区域:从区域内每一像素出发,可通过八个方向,即上.下.左.右.左上.右上.左下.右下移动的

UVa 572 - Oil Deposits【图DFS】

Oil Deposits 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

UVA 572 Oil Deposits油田(DFS求连通块)

UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Description Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M

Uva 572 Oil Deposits

Oil Deposits Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangul

UVa 572 Oil Deposits (Floodfill &amp;&amp; DFS)

题意 :输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横竖以及对角方向),就是说它们属于同一个八连块. 分析 :可以考虑种子填充深搜的方法.两重for循环枚举所有的点,然后只要是@点且还没被染色过则从这个点出发到达相邻的点染成同样的色(这里的颜色可以用不同的数字来表示),然后每一块@的联通块都能通过这种方式求出来,详情可以参考Wiki百科的动画帮助理解=>http://en.widipedia.org/wiki/

UVa 572 Oil Deposits(简单DFS)

题意: 给出油田的数量,八连通的“@”认为是一个油田.和POJ 2386 Lake Counting这个题几乎一样. 直接上代码: #include<iostream> using namespace std; char maze[105][105]; int n,m; void dfs(int x,int y) { maze[x][y]='*'; for(int dx=-1;dx<=1;dx++) for(int dy=-1;dy<=1;dy++) { int nx=x+dx,n

UVa 572 - Oil Deposits (简单dfs)

Description GeoSurvComp地质调查公司负责探测地下石油储藏. GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块.他们通过专业设备,来分析每个小块中是否蕴藏石油.如果这些蕴藏石油的小方格相邻,那么他们被认为是同一油藏的一部分.在这块矩形区域,可能有很多油藏.你的任务是确定有多少不同的油藏. Input 输入可能有多个矩形区域(即可能有多组测试).每个矩形区域的起始行包含m和n,表示行和列的数量,1<=n,m<=100,如果m =0表示输入的结束,