油田 bfs

 题目

eoSurvComp地质调查公司负责探测地下石油矿床。eosurvcompworks withone elgerectangularregionoflandatatime,andcreate a agridthat平分土地成许多小块。然后对每个地块分别进行分析,利用传感设备确定地块是否含有石油。一块含有石油的地块叫做“口袋”。如果两个储集层相邻,那么它们是同一油藏的一部分。石油蕴藏量可能非常大,可能包含许多小洞。你的工作是确定一个网格中包含多少不同的石油储量。输入输入文件包含一个或多个网格。每个网格以包含m和n的行开始,这是网格中的行数和列数,由单个空间分隔。如果m = 0结束的信号输入否则1≤≤100和1≤n≤100。接下来是m行,每个行包含n个字符(不包括行尾字符)。每个角色对应一个情节,要么是“”,代表没有油,要么是“”,代表一个油袋。输出每个网格,输出不同的石油储量的数量。如果两个不同的储集层水平相邻、垂直相邻或对角相邻,那么它们就是同一油藏的一部分。一个油藏的容量不会超过100个口袋。


样例输入


1


*

 

3个5


* @ * @ *


* * @ * *


* @ * @ *


1 8


@@ * * * * @ *


5个5


* * * * @


* @@ * @


* @ * * @


@@@ * @


@@ * * @


0 0


样例输出


0


1


2


2

英文:

The eoSurvComp geologic survey company is responsible for detecting underground oil deposits. eoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides the land into numerous suare plots. It then analyes each plot separately, using sensing euipment 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 uite 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 containsone 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 horiontally, 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
#include
#include
using namespace std;
char a[109][109];int book[109][109];int x1,y1;
struct node
{
	int x,y;
	node(){}
	node(int xx,int yy): x(xx),y(yy){}
};
void bfs(int x,int y)
{
	queueA;
	A.push(node(x,y));
	while(!A.empty())
	{
	node u=A.front();
	A.pop();
	for(int i = u.x-1;i <= u.x+1;i++)
	{
		for(int j = u.y-1;j <= u.y+1;j++)
		{
		if(i < 0 || i >= x1 || j < 0 || j >= y1)
		{
			continue;
		}
		if(a[i][j] == '@' && book[i][j] == 0)
		{
			book[i][j] = 1;
			A.push(node(i,j));
		}
		}	
	}
	} 
	
}
int main()
{
	while(1)
	{
		memset(a,0,sizeof(a));
		memset(book,0,sizeof(book));
		scanf("%d%d",&x1,&y1);
		if(x1 == 0 && y1 == 0)
		{
			break;
		}int y=0;
		for(int i=0;i>a[i][j];
				
			}
		}
		for(int i=0;i

 

思路:简单模板题直接套之后for循环从有油田的地方开始判断dfs进一会算一个判断几个dfs即使几个油田
 

 

 

你可能感兴趣的:(bfs)