油田 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<iostream>
#include<queue>
#include<string.h>
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)
{
	queue<node>A;
	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<x1;i++)
		{
			for(int j=0;j<y1;j++)
			{
				cin>>a[i][j];
				
			}
		}
		for(int i=0;i<x1;i++)
		{
			for(int j=0;j<y1;j++)
			{
				if(a[i][j] == '@' && book[i][j] == 0)
				{
					bfs(i,j);
					y++;
				}
			}
		}
		
		printf("%d\n",y);	
	}
	
	return 0;
}

 

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

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

-lyslyslys

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值