admin管理员组

文章数量:1648961

Oil Deposits

Time Limit: 1000MSMemory Limit: 10000K
Total Submissions: 27748Accepted: 14096

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

Sample Input

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

Sample Output

0
1
2
2

 

#include <iostream>
#include <cstring> 

using namespace std;
char p[110][110];
int book[110][110];//用来标记是否之前已经算进去了
int m,n,cnt=0;
void dfs(int a,int b, int cnt)
{
	if(a<0||b<0||a>=m||b>=n||p[a][b]!='@'||book[a][b]!=0)//结束条件
		return ;
	book[a][b]=1;//将这个标记用过
	for(int i=-1;i<=1;i++)//-1到1是用来标记8个方向
		for(int j=-1;j<=1;j++)
			if(i!=0||j!=0)
				dfs(a+i,b+j,cnt);
	
}

int main()
{
	
	while(cin >> m >> n,n!=0&&m!=0)
	{
		cnt=0;
		memset(book,0,sizeof book);
		for(int i=0; i<m; i++)
			for(int j=0; j<n; j++)
				cin >> p[i][j];
		for(int i=0; i<m; i++)
		{
			for(int j=0; j<n; j++)
			{
				if(p[i][j]=='@'&&book[i][j]==0)
					dfs(i,j,++cnt);
					
				
			}
		}
	cout << cnt << endl;
		


	}




}

参考:

(3条消息) 问题 C: 油田问题_唐傘皮的博客-CSDN博客https://blog.csdn/qq_43520913/article/details/106149116?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164834523116782094862696%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fall.%2522%257D&request_id=164834523116782094862696&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~first_rank_ecpm_v1~rank_v31_ecpm-1-106149116.142^v5^pc_search_result_cache,143^v6^register&utm_term=%E6%B2%B9%E7%94%B0%E7%81%8C%E6%BA%89&spm=1018.2226.3001.4187

本文标签: 油田