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 = -1; i <= 1; ++i)
            for(int j = -1; j <= 1; ++j)
                if(mat[r + i][c + j] == '@')
                    dfs(r + i, c + j);
    }
    return 1;
}

int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m) , m)
    {
        for(int i = 1; i <= n; ++i)
            scanf("%s", mat[i] + 1);

        int ans = 0;
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= m; ++j)
                ans += dfs(i, j);
        printf("%d\n", ans);
    }
    return 0;
}

 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

时间: 2024-10-10 23:59:00

UVa 572 Oil Deposits(DFS求8连通块)的相关文章

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.

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

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

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     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 油田(DFS求连通块)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=513 终于开始接触图了,恩,开始接触DFS了,这道题就是求连通分量,比较简单. 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 int m, n; //记录连通块的数量 6 cha

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】联通块问题

题目链接 题目大意: 求图中@连通块的个数,只要两个@相邻就算它们是连通的(斜方向的也算相邻) #include <stdio.h> #include <string.h> char map[110][110]; int vis[110][110]; int n, m, ans; int dir[8][2] = { {0,1},{1,1},{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1} }; void dfs(int a,int b) { int

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/