Oil Deposits(深搜初体验)

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 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.


问题描述:
GeoSurvComp地质调查公司负责检测地下油藏。GeoSurvComp一次与一个大的矩形区域一起工作,并创建一个网格,将网格划分为多个方块。然后分别分析每个地块,使用传感设备确定该地块是否含有油。含油的情节被称为口袋。如果两个口袋相邻,则它们是同一个油藏的一部分。油藏可能相当大,可能含有大量的口袋。你的工作是确定网格中包含多少个不同的油藏。
Input:
输入文件包含一个或多个网格。每个网格以含有m和n的行开始,网格中的行和列的数量由一个空格分隔。如果m = 0,则表示输入结束; 否则1 <= m <= 100且1 <= n <= 100.之后是每行n个字符的m行(不包括行尾字符)。每个字符对应一个图,并且代表没有油的‘*’或代表油袋的‘@’。

Output:
对于每个电网,输出不同油量的数量。如果两个不同的口袋是水平,垂直或对角相邻的,则它们是同一个油藏的一部分。一个油藏不会超过100个口袋。

Source:

Mid-Central USA 1997

问题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241

考察点:深搜+地图

易错点:忘记将已经搜过的口袋设置为普通地 或者是吃掉了换行。

解题思路
明确是深搜问题然后写出判断方向后继续搜索。存入地图找到深搜的开始点,开始搜索。

代码:

#include<bits/stdc++.h>
int dir[8][2]={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};//定义方向
char mapp[105][105];
int n,m;

void dfs(int x,int y)//深搜
{
    if(mapp[x][y]=='@')//将搜过的口袋标记
        mapp[x][y]='*';
    for(int i=0;i<8;i++)//八个方向搜索
    {
        int a=dir[i][0]+x;//横坐标改变
        int b=dir[i][1]+y;//纵坐标
        if(mapp[a][b]=='@'&&a<n&&a>=0&&b<m&&b>=0)//如果是口袋且横纵坐标都在范围内
            dfs(a,b);//进行搜索
    }
}
int main()
{
    while(scanf("%d %d",&n,&m)&&n!=0&&m!=0)
    {
        int sum=0;//油藏数初始化为0
        getchar();//吞行

        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
                scanf("%c",&mapp[i][j]);
            getchar();//吞行
        }
        for(int i=0;i<n;i++)
            for(int j=0;j<m;j++)
        {
            if(mapp[i][j]=='@')//遇到口袋进行搜索
            {
                dfs(i,j);
                sum++;//深搜到底后,出来使得油藏数目增加
            }
        }
        printf("%d\n",sum);
    }
    return 0;

}

示例:

输入:

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

结果:

0
1
2
2

运行结果:

这里写图片描述

猜你喜欢

转载自blog.csdn.net/shuisebeihuan/article/details/80505273