Mid-Central USA 1997--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

首先知道油田的整体面积可以8个方向进行遍历,所以创建两个数组x,y进行坐标的获得。

int dx[8]={-1, -1, 0, 1, 1, 1, 0, -1};//上-右上-右-右下-下-左下-左-左上 
int dy[8]={ 0,  1, 1, 1, 0,-1,-1, -1};

BFS里面用二维数组进行遍历

for(int i=1;i<=M;i++)
for(int j=1;j<=N;j++) 
if(map[i][j]=='@'&&!visited[i][j])
find_oil(i,j);

若是油田就进入find_oil,用队列来实现,head=0,tail=1,

struct queue{
int xx;int yy;
}que[10100];

(1)未标记过的话,就进行广度优先遍历,

while(head<tail)是一个大的循环,

(a)

for(int i=0;i<8;i++)

{x1=x+dx[i];y1=y+dy[i];//八个方向 

if(x1>=1&&x1<=M&&y1>=1&&y1<=N)//判断是否越界

以上都满足并且是油田且未遍历过,就进入队列,并标记走过

(b)以上不满足的话,则直接head++,直到head>tail

(2)若标记过,直接对油田标记走过,并返回。

用一个visited数组来标记走过的油田。



#include<iostream>
#include<cstring>
using namespace std;
int M,N,total;
char map[110][110];
int visited[110][110];
int dx[8]={-1, -1, 0, 1, 1, 1, 0, -1};//上-右上-右-右下-下-左下-左-左上 
int dy[8]={ 0,  1, 1, 1, 0,-1,-1, -1};
struct queue{
	int xx;int yy;
}que[10100];
void find_oil(int x,int y)
{
	int x1,y1;
	int head,tail;
	if(!visited[x][y])
	{
		total++;//第一次遍历  则加1    地图标记遍历过了 
		visited[x][y]=1;
	}
	if(visited[x][y]==1)//已经标记过,找周围的油田 
	{
		head=0,tail=1;
		que[0].xx=x;  //  注意刚才用的是  que[head].xx=x; 
		que[0].yy=y;  //但是下面的循环会让head的值并非是0,所以 
		while(tail>head)
		{
			x=que[head].xx; 
			y=que[head].yy; 
			for(int i=0;i<8;i++)
			{
				x1=x+dx[i];y1=y+dy[i];//八个方向 
				if(x1>=1&&x1<=M&&y1>=1&&y1<=N)//判断是否越界 
					if(map[x1][y1]=='@'&&!visited[x1][y1])
					{
						que[tail].xx=x1;
						que[tail].yy=y1;
						visited[x1][y1]=1;
						tail+=1;
					}
			}
			head++;
		}
	}
}
void BFS()
{
	for(int i=1;i<=M;i++)
		for(int j=1;j<=N;j++) 
			if(map[i][j]=='@'&&!visited[i][j])
				find_oil(i,j);
}
int main()
{
	ios::sync_with_stdio(false);
	while(cin>>M>>N)
	{
		cin.get();
		if(M<=0||N<=0) break;
		total=0;
		memset(map,0,sizeof(map));
		memset(visited,0,sizeof(visited));//地图标记清0 
		for(int i=1;i<=M;i++)
			for(int j=1;j<=N;j++)
				cin>>map[i][j];
		/*for(int i=1;i<=M;i++)
		{
			for(int j=1;j<=N;j++)
				cout<<map[i][j];
			cout<<endl;
		}*/
		BFS();
		cout<<total<<endl;
	}
	return 0;
 } 


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值