Oil Deposits之DFS解题报告

Oil Deposits

Time Limit: 2000/1000 MS(Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7288    Accepted Submission(s): 4256

Problem Description

The GeoSurvComp geologic survey company is responsiblefor detecting underground oil deposits. GeoSurvComp works with one largerectangular region of land at a time, and creates a grid that divides the landinto numerous square plots. It then analyzes each plot separately, usingsensing equipment to determine whether or not the plot contains oil. A plotcontaining oil is called a pocket. If two pockets are adjacent, then they arepart of the same oil deposit. Oil deposits can be quite large and may containnumerous pockets. Your job is to determine how many different oil deposits arecontained in a grid.

 

 

Input

The input file contains one or more grids. Each gridbegins with a line containing m and n, the number of rows and columns in thegrid, 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 mlines of n characters each (not counting the end-of-line characters). Eachcharacter corresponds to one plot, and is either `*', representing the absenceof oil, or `@', representing an oil pocket.

 

 

Output

For each grid, output the number of distinct oildeposits. Two different pockets are part of the same oil deposit if they areadjacent horizontally, vertically, or diagonally. An oil deposit will notcontain more than 100 pockets.

 

 

Sample Input

1 1

*

3 5

*@*@*

**@**

*@*@*

1 8

@@****@*

5 5

****@

*@@*@

*@**@

@@@*@

@@**@

0 0

 

 

Sample Output

0

1

2

2

 

 

Source

Mid-CentralUSA 1997

 

 

题目解析:

       题目大意是说有一块矩形的区域,其中可能会存在有油田,用‘@’表示,‘*’表示无油田,如果油田存在的位置与另一块油田在水平、竖直、或者对角相邻,则表示同一块油田。输入数据为m x n的矩阵,即该矩阵为m行n列,判断并输出其中存在的油田数。

大体思路:

       该题采用深度优先搜索,从深度优先的策略上看就知道深搜一般是用递归来实现;深度优先搜索的框架很简单:
void Dfs ( int n )
{
     if ( 满足结束条件,即搜索到终点 )
         return ;
     else
         Dfs ( n + 1 );
}

双重循环遍历,如果扫描到的位置是油田,则先将其值改为‘*’,在对其周边的八个位置进行遍历,DFS()递归调用,判断是否存在油田。

代码如下:

#include <iostream>
#include <string.h>
#include <queue>
#define N 105
using namespace std;

char map[N][N];
int n,m,sum;

void DFS(int i,int j)
{
    if(i<0||j<0||i>=m||j>=n||map[i][j]!='@')        //判断map[i][j],如果越界或不是油田,直接返回
        return;
    else            //将当前油田位置标记为'*‘,并判断该油田周边的八个方向是否存在油田
    {
        map[i][j]='*';
        DFS(i,j+1);
        DFS(i-1,j+1);
        DFS(i-1,j);
        DFS(i-1,j-1);
        DFS(i,j-1);
        DFS(i+1,j-1);
        DFS(i+1,j);
        DFS(i+1,j+1);
    }
}
int main()
{
    int i,j;
    while(cin>>m>>n&&(m||n))
    {
        sum=0;
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
                cin>>map[i][j];
        for(i=0;i<m;i++)
            for(j=0;j<n;j++)
            {
                if(map[i][j]=='@')      //存在油田,调用DFS(),深度优先遍历
                {
                    DFS(i,j);
                    sum++;
                }
            }
        cout<<sum<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值