Oil Deposits(油田)

题目:

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides 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 containsone 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

题意:

和迷宫的输入差不多,‘*’是墙,‘@’是油田,以一个油田为中心,如果它的东南西北一个各个角落,一共八个方向也有油田的话,这些油田就算作一个油田。

分析:

查找到‘@’的位置,找到一个,油田加1,对找到的‘@’的四周进行DFS查找,刚开始我以为和迷宫的做法差不多,我把查找过的地方用一个数组标记一下,后来测试结果不对,

才发现在某一次存在题意中的相邻油田时,在主函数中会加1,而实际上这块油田我已经计算进去了,这样就重复了。正确的做法应该是把计算进去的那块油田变为‘*’,这样

就不存在重复计算了!!!

AC代码:

#include<iostream>
#include<cstring>
#include<cstdio>
char a[102][102];
int row,col;
int dir[8][2]=
{
    {1,0},
    {1,1},
    {1,-1},
    {0,-1},
    {0,1},
    {-1,0},
    {-1,1},
    {-1,-1}
};
using namespace std;
void dfs(int i,int j)
{
    a[i][j]=‘*‘;
    for (int k=0;k<8;k++)
    {
        int x=i+dir[k][0];
        int y=j+dir[k][1];
        if (x>=1&&x<=row&&y>=1&&y<=col&&a[x][y]==‘@‘)
            dfs(x,y);
    }
    return ;
}
int main()
{
    while ((cin>>row>>col)&&(row!=0||col!=0))
    {
        int c=0;
        getchar();
        for (int i=1;i<=row;i++)
            for (int j=1;j<=col;j++)
            cin>>a[i][j];
        for (int i=1;i<=row;i++)
            for (int j=1;j<=col;j++)
            if (a[i][j]==‘@‘)
            {
                dfs(i,j);
                c++;
            }
        cout << c << endl;
    }
    return 0;
}
时间: 2024-10-11 06:18:22

Oil Deposits(油田)的相关文章

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

油田(Oil Deposits)-用DFS求连通块

[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个m行n列的字符矩阵,统计字符"@"组成多少个八连块.如果两个字符"@"所在的格子相邻(横.纵或者对角线方向),就说它们属于一个八连块.例如,下图中有两个八连块. 一.分析 1.1 整体思路 图也有DFS和BFS遍历.由于DFS更容易编写,一般用DFS查找连通块:从每个&

HDU_1241 Oil Deposits(DFS深搜)

Problem Description 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 pl

暑假集训(1)第七弹 -----Oil Deposits(Poj1562)

Description 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

hdoj 1241 Oil Deposits

Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 16542    Accepted Submission(s): 9500 Problem Description The GeoSurvComp geologic survey company is responsible for detecting underg

HDU 1562 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 ana

hdu 1241 Oil Deposits

题意:用广度优先搜索 //c++写输入时有问题 1)这个是深搜 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> /* map数组是用来装字符的 n,m提高作用域,使訪问的权限变高 dir方便广度优先搜索,由于要搜索8个方向.这样做最方便 */ char map[101][101]; int n,m; int dir[8][2]={{-1,-1},{0,-

2016HUAS暑假集训训练题 G - Oil Deposits

Description 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

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