uva 572

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进行搜素,注意边界,此题跟poj2386基本一样。
代码:
#include<cstdio>
using namespace std;
const int maxn=105;
int N,M;
char field[maxn][maxn];
void dfs(int x,int y)
{
    field[x][y]='*';
    for(int dx=-1; dx<=1; dx++)
    {

        for(int dy=-1; dy<=1; dy++)
        {
            int nx=x+dx,ny=y+dy;
            if(nx>=0&&nx<N&&ny>=0&&ny<M&&field[nx][ny]=='@')
                dfs(nx,ny);

        }
    }
    return ;
}
int main()
{

   while (scanf("%d%d",&N,&M)&&N&&M)

{
     int sum=0;
    for(int i=0; i<N; i++)
            scanf("%s",&field[i]);//不能用scanf("%s",&field[i][j]),scanf会读回车,会乱了
    for(int i=0; i<N; i++)
    {
        for(int j=0; j<M; j++)
            if(field[i][j]=='@')
            {
                dfs(i,j);
                sum++;
            }
    }
    printf("%d\n",sum);

}

return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-13 08:58:05

uva 572的相关文章

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)

 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     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(DFS求连通块+种子填充算法)

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

2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小时连题解都出来了··· A-烤肉拌饭 ( uva 572) 就是求联通块的数量啊,刚学dfs的时候做的那种! 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <ios

UVA 572 油田连通块-并查集解决

题意:8个方向如果能够连成一块就算是一个连通块,求一共有几个连通块. 分析:网上的题解一般都是dfs,但是今天发现并查集也可以解决,为了方便我自己理解大神的模板,便尝试解这道题目,没想到过了... 1 #include <cstdio> 2 #include <iostream> 3 #include <sstream> 4 #include <cmath> 5 #include <cstring> 6 #include <cstdlib&

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 油田

题目:相当于有一个m行n列的格子矩阵,每个格子要么@要么*,两个格子是连通的当且仅当这两个格子都是@.且一个格子和另一个是水平.垂直或对角线相邻,即一个格子在另一个格子的周围八个格子范围内.最后求连通块的个数. 思路:和书上例题黑白格子一样,只是一个01,一个*@,稍微改一下就可以了.即通过dfs(x,y)递归地深度优先遍历. Code: #include<stdio.h> #include<string.h> #define MAX 110 void dfs(int x,int