Oil Deposits石油矿问题

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.

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

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.

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

//输入的结束;否则为1=< = 100和1 < < = < = 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.

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

Sample Input

1 1

3 5

@@*

@

@@*

1 8

@@***@

5 5

****@

@@@

*@**@

@@@*@

@@**@

0 0

Sample Output

0

1

2

2

感谢搜狗翻译和不太靠谱的百度翻译的支持(狗头)
思路:刚接触到这个问题时,尝试过用一个数组来储存石油矿的位置,再用另一个数组来对探索过的油矿进行标记,于是在重复搜索时遇到不少繁琐的步骤。后来意识到,可以在原来的数组中改变石油矿对应元素的值,来将它“挖”出来,这样在重复搜索的时候就不用特意避开这个区域了,然后一步步改进简化了步骤,就变成了简简单单的dfs问题了。

#include <bits/stdc++.h> 
#include <iostream>
using namespace std;
int n,m;
char  a[105][105];//m,n的值都小于100
void dfs(int i,int j)
{
	if(a[i][j]!='@'||i<0||j<0||i>=m||j>=n)
	    return;
	else
	{
		a[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,sum;
	while(~scanf("%d %d",&m,&n)&&(m||n))//m或n为0时循环不执行
	{
	    sum=0;	
		for(i=0;i<m;i++)scanf("%s",a[i]);		
	    for(i=0;i<m;i++)	
	    	for(j=0;j<n;j++)	    	
	    	  if(a[i][j]=='@')
	    		{
	    			dfs(i,j);//执行dfs会探索周围所有相连的石油
	    			sum++;//计数
				}			

		printf("%d\n",sum);
	}
	return 0;
}

将已探明的石油在原数组标记出来,大大简化了思路,甚至让问题变得过于简单,而使用另一个数组来标记石油则需要规避已发现的石油,让问题变得复杂许多,在动手书写程序之前对问题多加思考也很重要。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值