题解—detecting underground oil deposits

Problem Description

The GeoSurvComp geologicsurvey 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 equipmentto 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 containnumerous pockets. Your job is to determine how many different oil deposits are contained in a grid. <br>

geosurvcomp地质调查公司负责探测地下石油储量。GeoSurvComp的作品与一个大的矩形区域的土地在一个时间,并创建一个网格把土地分成很多广场地块。然后,它分别分析每个情节,使用感测设备,以确定是否含有石油阴谋。一块含有石油的地块叫做口袋。如果两个口袋相邻,那么它们是同一个石油矿床的一部分。石油储量可能相当大,可能包含许多口袋。你的工作是确定网格中包含多少种不同的石油储量。<br>

Input

The input file contains one ormore grids. Each grid begins with a linecontaining m and n, the number of rows and columns in the grid, separated by asingle 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 charactercorresponds to one plot, and is either `*', representing the absence of oil, or `@', representing an oil pocket.<br>

// 输入文件包含一个或多个网格。每个网格以包含mn的行开始,网格中的行数和列数由一个空格分隔。如果m0,则表示

//输入的结束;否则为1< = 1001 < < = < = 100。下面是每行n个字符的m行(不包括行字符的结尾)。每一个字符对

//应一个图,并且是“*”,表示没有油,或“@”,表示一个油袋。

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 notcontain more than 100 pockets.<br>

//对于每一个网格,输出不同的油藏数量。两个不同的口袋是同一油藏的一部分,如果它们是水平的、垂直的或对角的。不超过100个口袋。                         

Sample Input

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

Sample Output

0
1
2
2

//原谅我的粗糙的百*翻译,,实在是太多的生词看不下去了(羞愧&&英语会努力的)

解题思路:

一开始想练一练广搜来着,结果敲着敲着又变成了深搜,,看题解时发现了一个挺好的遍历八个方向的方法,如下:

for(int i=-1;i<=1;i++)
 for(int j=-1;j<=1;j++)
  if(i!=0||j!=0)
  {
  
  }

PS:

1-输入的格式是字符串而不是字符

AC.1

#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
char map[101][101];
int m=0,n=0,ans;
int mark[101][101]={0},
    dx[]={-1,-1,-1,0,0,1,1,1},
    dy[]={-1,0,1,-1,1,-1,0,1};

void search(int x,int y,int id)
{
    if(x<0||x>=m||y<0||y>=n)
        return ;
    if(mark[x][y]>0||map[x][y]!='@')
        return ;
    mark[x][y]=id;
    for(int i=0;i<=7;i++)
        search(x+dx[i],y+dy[i],id);
}

int main()
{
    while(scanf("%d%d",&m,&n)&&(m+n)!=0)
    {
        ans=0;//清零
        memset(mark,0,sizeof(mark));
        for(int i=0;i<m;i++)
                scanf("%s",map[i]);//注意其中没有空格,需用字符串输入

        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
            {
                if((mark[i][j]==0)&&(map[i][j]=='@'))//没有被访问过
                search(i,j,++ans);
            }
        cout<<ans<<endl;
        n=0;
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值