ZOJ 1709 Oil Deposits(dfs,连通块个数)

Oil Deposits


Time Limit: 2 Seconds      Memory Limit: 65536 KB


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



Source: Mid-Central USA 1997

分析:

dfs水题,问你@连通块的个数

@可以向8个方向扩展

和油田问题属于同一类问题

传送门(点我)

code:

#include<bits/stdc++.h>
using namespace std;
#define max_v 105
int n,m;
char a[max_v][max_v];
int used[max_v][max_v];
int dir[8][2]={{0,1},{0,-1},{1,0},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1}};
void dfs(int x,int y,int z)
{
    if(x<0||x>=n||y<0||y>=m)
        return ;
    if(used[x][y]>0||a[x][y]!=‘@‘)
        return ;
    used[x][y]=z;
    for(int i=0;i<8;i++)
        dfs(x+dir[i][0],y+dir[i][1],z);
}
int main()
{
    while(cin>>n>>m)
    {
        if(m==0&&n==0)
            break;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
               cin>>a[i][j];
            }
        }
        memset(used,0,sizeof(used));
        int c=0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            {
                if(used[i][j]==0&&a[i][j]==‘@‘)
                    dfs(i,j,++c);
            }
        }
        printf("%d\n",c);
    }
}

原文地址:https://www.cnblogs.com/yinbiao/p/9391612.html

时间: 2024-10-12 11:57:14

ZOJ 1709 Oil Deposits(dfs,连通块个数)的相关文章

ZOJ - 1709 Oil Deposits

油田问题,有点像图像处理里的区域生长问题,找油田块数.BFS,DFS都可以. 1 /*BFS*/ 2 #include<stdio.h> 3 #include<string.h> 4 const int maxn=100+5,maxm=1000; 5 int m,n,vis[maxn][maxn],mat[maxn][maxn],dir[10][3]={{1,0},{1,-1},{0,-1},{-1,-1},{-1,0},{-1,1},{0,1},{1,1}}; 6 char s[

Oil Deposits DFS FloodFill漫水填充法求连通块问题

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

PKU 1562/HDU 1241 Oil Deposits(原油有多少块区域---BFS,DFS)

Oil Deposits Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region o

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.

CSUOJ 1601 War (离线并查集求连通块个数)

1601: War Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 130  Solved: 38 [Submit][Status][Web Board] Description AME decided to destroy CH's country. In CH' country, There are N villages, which are numbered from 1 to N. We say two village A and B ar

P1197 [JSOI2008]星球大战 [删边求连通块个数]

展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧道互相直接或间接地连接. 但好景不长,很快帝国又重新造出了他的超级武器.凭借这超级武器的力量,帝国开始有计划地摧毁反抗军占领的星球.由于星球的不断被摧毁,两个星球之间的通讯通道也开始不可靠起来. 现在,反抗军首领交给你一个任务:给出原来两个星球之间的以太隧道连通情况以及帝国打击的星球顺序,以尽

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 =

DFS(连通块) HDU 1241 Oil Deposits

题目传送门 1 /* 2 DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 3 */ 4 /************************************************ 5 Author :Running_Time 6 Created Time :2015-8-4 10:11:11 7 File Name :HDOJ_1241.cpp 8 ************************************************/ 9 10

DFS(连通块) ZOJ 2743 Bubble Shooter

题目传送门 题意:从炮台射出一个球,三个及以上颜色相同的会掉落,问最后会掉落多少个球 分析:先从炮台找一个连通块,然后与顶部连接的连通块都不会掉落,剩下的就是炮台射出后跟随掉落的. #include <bits/stdc++.h> const int N = 100 + 5; char str[N][N]; int H, W, h, w; int ans; bool check(int x, int y) { if (x < 1 || x > H || y < 1 || y