(UVA 572)Oil Deposits(BFS)

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
题目大意:
勘探公司对一块土地进行石油勘探,你的任务是帮助勘探公司统计其中的油田数量@为油田
为土地。@相连在一块的算一整块油田,@相连的方向可以是“上”“下”“左”“右”“左上”“左下”“右上”“右下”八个方向,第一行输入两个数字表示土地的长和宽(输入为0 0时结束)下面n行输入土地的状态。
分析:
运用广度优先搜索,遇到@就将对应坐标放入队列,并将此点标记已搜索,取出此点坐标后对此点进行八个方向搜索 ,遇到@后将对应坐标放入队列并重复之前的操作直到队列为空。此时一整块就算统计完了,数量+1.再去其他未标记的点进行搜索。
例如:
样例1 样例2 在这里插入图片描述

#include<iostream>
#include<queue>
#include<string.h>
using namespace std;
int vis[105][105],sum=0;//用于记录标记,此点是否被访问过
char map[105][105];//用于存放地图
void bfs(int x,int y)//BFS搜索
{
	queue<int> q;
	q.push(x);
	q.push(y);
	int x1,y1;
	while(!q.empty())//队列不为空时说明此点还没有完全延伸出来
	{
		x1=q.front();
		q.pop();
		y1=q.front();
		q.pop();
		if(map[x1+1][y1]=='@'&&vis[x1+1][y1]==0)//枚举八个方向,由于笔者每次用for循环总是出错,就运用
		{																		//了比较笨但较稳妥的方法,望dalao海涵
			q.push(x1+1);
			q.push(y1);
			vis[x1+1][y1]=1;
		}
		if(map[x1-1][y1]=='@'&&vis[x1-1][y1]==0)
		{
			q.push(x1-1);
			q.push(y1);
			vis[x1-1][y1]=1;
		}
		if(map[x1][y1+1]=='@'&&vis[x1][y1+1]==0)
		{
			q.push(x1);
			q.push(y1+1);
			vis[x1][y1+1]=1;
		}
		if(map[x1][y1-1]=='@'&&vis[x1][y1-1]==0)
		{
			q.push(x1);
			q.push(y1-1);
			vis[x1][y1-1]=1;
		}
		if(map[x1+1][y1+1]=='@'&&vis[x1+1][y1+1]==0)
		{
			q.push(x1+1);
			q.push(y1+1);
			vis[x1+1][y1+1]=1;
		}
		if(map[x1+1][y1-1]=='@'&&vis[x1+1][y1-1]==0)
		{
			q.push(x1+1);
			q.push(y1-1);
			vis[x1+1][y1-1]=1;
		}
		if(map[x1-1][y1+1]=='@'&&vis[x1-1][y1+1]==0)
		{
			q.push(x1-1);
			q.push(y1+1);
			vis[x1-1][y1+1]=1;
		}
		if(map[x1-1][y1-1]=='@'&&vis[x1-1][y1-1]==0)
		{
			q.push(x1-1);
			q.push(y1-1);
			vis[x1-1][y1-1]=1;
		}
	}
	sum++;//队列为空时,一整块油田就统计完了,此时油田数+1
}
int main()
{
	int n,m;
	while(cin>>n>>m)//输入土地的长和宽
	{
		if(n==0&&m==0)
		break;
		char a[105];
		memset(vis,0,sizeof(vis));
		sum=0;
		for(int i=1;i<=n;i++)
		{
			cin>>a;
			for(int j=1;j<=m;j++)
			map[i][j]=a[j-1];
		}
		for(int i=1;i<=n;i++)//对每个点都进行一次检测是油田且没标记过就进行搜索
			for(int j=1;j<=m;j++)
			{
				if(vis[i][j]==0&&map[i][j]=='@')
				{
					vis[i][j]=1;	
					bfs(i,j);
				}
			}
		cout<<sum<<endl;//全部统计完后就输出油田数量
	}
		
}

猜你喜欢

转载自blog.csdn.net/basketball616/article/details/85369822
今日推荐