VirtualJudge-Oil Deposits

版权声明:都是自己写的喔~ https://blog.csdn.net/qq_42811706/article/details/89004383

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

据说这个题是深搜的经典入门题,主函数输入地图,进行循环判断当前点是否有油,有的话以从该点进入深搜,执行深搜函数

dfs中,首先数组存储下一步要走的方向,这个题有八个方向,对每个方向判断是否符合条件,符合的进行递归。
这个题没有用到book数组进行标记,直接把已经走过的油田记录改变了,防止重复。

代码

#include<iostream>
#include<cstdio>
using namespace std;
char mp[105][105];
int step[8][2]={1,1,1,0,1,-1,0,1,0,-1,-1,1,-1,0,-1,-1};
int m,n; 
void dfs(int x,int y)
{
	mp[x][y]='*';
	for(int i=0;i<8;i++)
	{
		int nx=x+step[i][0];
		int ny=y+step[i][1];
		if(nx<0 || nx>=m || ny<0 || ny>n)
			continue;
		if(mp[nx][ny]=='@')
		{
			dfs(nx,ny);
		}
	}
}
int main ()
{
	while(scanf("%d %d",&m,&n),m!=0)
	{
		int sum=0;
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
				cin>>mp[i][j];
		}
		for(int i=0;i<m;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(mp[i][j]=='@')
				{
					dfs(i,j);
					sum++;
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}

首先也说一下决定居续写,因为今年蓝桥杯意料之外的进了国赛,指导老师给开会后,发现自己目前水平去国赛就是给大佬们当分母的,所以准备用博客当笔记本,记录这段时间的学习

猜你喜欢

转载自blog.csdn.net/qq_42811706/article/details/89004383