HDU1241-Oil Deposits(DFS)

Oil Deposits

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

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.

Input
The input file 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
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 not contain more than 100 pockets.

Sample Input
1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@
@
@@**@
0 0

Sample Output
0
1
2
2

问题描述
GeoSurvComp地质勘测公司负责检测地下油藏。 GeoSurvComp一次处理一个大矩形区域的土地,并创建一个将土地划分为多个正方形图的网格。然后,它使用传感设备分别分析每个地块,以确定该地块是否包含油。包含油的地块称为矿穴。如果两个凹坑相邻,则它们是同一油藏的一部分。积油可能很大,可能包含许多凹穴。您的工作是确定网格中包含多少种不同的油藏。

输入值
输入文件包含一个或多个网格。每个网格均以包含m和n的行开始,网格中的行和列数为m和n,以单个空格分隔。如果m = 0,则表示输入结束;否则为1 <= m <= 100和1 <= n <=100。紧随其后的是m行,每行n个字符(不计算行尾字符)。每个字符对应一个图,并且为*'表示没有油,或者为@'表示油囊。

输出量
对于每个网格,输出不同的油藏数量。如果两个不同的油藏在水平,垂直或对角线上相邻,则它们是同一油藏的一部分。积油最多可容纳100个口袋。
Sample Input
1 1
*
3 5
@@*
@
@@*
1 8
@@***@
5 5
****@
@@@
@**@
@@@
@
@@**@
0 0
Sample Output
0
1
2
2

题目链接
<WFU2019级第二周:简单搜索>

这道题是HDU的题目,典型的DFS的题目,题意让求出要塞的数量,也就是@的连续区域的数量,这个题我们可以遍历地图,如果碰到’@'就ans++,并以这个点进行深搜,将他周围的@全部踩成*,要注意的是 这里指的相连,是指8个方向,并不是四个方向,这里注意一下,写深搜就可以。AC代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int N=105;
char a[N][N];
int m,n;
int main()
{
	void dfs(int,int); 
	while(cin>>m>>n&&m)
	{
		for(int i=0;i<m;i++)
		  cin>>a[i];
		int ans=0;
		for(int i=0;i<m;i++)
		  for(int j=0;j<n;j++)
		    if(a[i][j]=='@')
			{
				ans++;//遍历地图统计答案
				dfs(i,j);
			}
	    cout<<ans<<endl;		
	}
	return 0;
}
void dfs(int x,int y)
{
	int next[8][2]={{1,0},{0,1},{-1,0},{0,-1},
	{1,-1},{-1,1},{1,1},{-1,-1}};//向8个方向扩展
	int nx,ny;
	for(int k=0;k<8;k++)
	{
		nx=x+next[k][0];
		ny=y+next[k][1];
		if(nx<0||nx>m-1||ny<0||ny>n-1)
		  continue;//防止越界
		if(a[nx][ny]=='@')
		{
		  a[nx][ny]='*';//将周围的区域置为*
		  dfs(nx,ny);
		}
	}
	return;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值