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

are adjacent horizontally, vertically, or diagonally. An oil deposit will not contain more than 100 pockets.

GeoSurvComp地质调查公司负责探测地下石油储量。GeoSurvComp 一次与一个大型矩形土地区域配合工作,并创建一个网格,将土地划分为多个方块。然后,它分别分析每个绘图,使用感应设备确定该地块是否含有油。含有油的地块称为口袋。如果两个口袋相邻,则它们是同一油藏的一部分。石油储量可能相当大,可能含有许多口袋。您的工作是确定网格中包含多少不同的油藏。
输入

输入包含一个或多个网格。每个网格以包含 m 和 n 的线开始,网格中的行和柱数由单个空间分开。如果m=0它表示输入的结束:否则 1 < = m < = 100 和 1 < = n < = 100 。下面是每个n字符的m行(不包括行尾字符)。每个字符对应于一个绘图,并且是"*",表示缺少油,或表示油袋的"@"。
 
输出

水平、垂直或对角线相邻。油藏不会包含超过100个口袋。


示例输入

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


样本输出

0
1
2
2


不同的坐标表示形式,搜索时坐标代表含义不同,具体看代码

题意描述:
给n*m个数据,*表示没有油,@表示有油,如果@水平、垂直或对角线相邻,代表一个油田,求油田个数

解题思路:
利用深搜dfs,找到@开始遍历,深搜分八个方向,当走到的地方没有超届并且没有被标记过,就将这个地方标记成没有油田,以该点为起点继续遍历,直到搜索完,输出油田个数。

AC代码
 

# include <cstdio>
# include <iostream>
using namespace std;
char abc[120][120];
int m,n;
int i,j,k;
int ans;
int a[8]={-1,-1,0,1,1,1,0,-1};//等同  int vis[8][2]={{-1,0},{-1,1},{0,1},{1,1},{1,0]},{1,-1},{0,-1},{-1,-1}}
int b[8]={0,1,1,1,0,-1,-1,-1};
void dfs(int x,int y)
{
    int k;
    if(x<0 || y<0)
    return;     //这里的return返回很重要!!回溯
    if(abc[x][y]=='@')
    {
    abc[x][y]='*';
    for(k=0;k<8;k++)
    dfs(x+a[k],y+b[k]);  //等同int dx=x+vis[i][0]; //int dy=y+vis[i][1];
}
return;
}
int main()
{
    int m,n;
    while(scanf("%d%d",&m,&n)!=EOF&&(m||n))
    {
        int i,j;
        int ans=0;
        for(i=0;i<m;i++)
        scanf("%s",abc[i]);
        for(i=0;i<m;i++)
        {
            for(j=0;j<n;j++)
            {
            if(abc[i][j]=='@')
            {
            ans++;
            dfs(i,j);
         }
    }
}

printf("%d\n",ans);

}
return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值