Oil Deposits UVA - 572

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.

Sample Input

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

Sample Output

0
1
2
2

HINT

此题重点在于对dfs算法的掌握,一个是记录原始数据的数组,一个是是否已经访问过的标志数组,在主函数里面对每一个点进行判断,如果没有被访问并且是@那么进行dfs,并将访问下标加一,以和上一个进行区别。那么在dfs里面的内容就是以给定的点为中心,将所有的可能的点进行判断访问。这样从主函数进入dfs再回来意味着将一个连通分量判断完成了。那么主函数将每一个元素都判断,肯定会求出所有的连通分量。

Accepted

#include <cstdio>
#include <cstring>
const int maxn = 105;
const int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
const int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
int n, m, s[maxn][maxn], idx[maxn][maxn];

void dfs(int x, int y, int cnt) {
    if (idx[x][y] != -1 ||  x < 0 || x >= n || y < 0 || y >= m) return;
    if (s[x][y] == '@') {
        idx[x][y] = cnt;
        for (int i = 0; i < 8; i++) {
            int x1 = dx[i] + x, y1 = dy[i] + y;
            dfs(x1, y1, cnt);
        }
    }
}

int main() {
    while (scanf("%d%d\n", &n, &m) == 2 && n && m) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++)
                s[i][j] = getchar();
            getchar();
        }
        int cnt = 0;
        memset(idx, -1, sizeof(idx));
        for (int i = 0; i < n; i++)
            for (int j = 0; j < m; j++)
                if (s[i][j] == '@' && idx[i][j] == -1)
                    dfs(i, j, ++cnt);
        printf("%d\n", cnt);
    }
    return 0;
}
posted @   布拉多1024  阅读(41)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示