杭电1241-Oil Deposits

Oil Deposits

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7705    Accepted Submission(s): 4510


Problem 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 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
题目意思:就给你由@(油田)和*(不是油田)组成n*m的网格,然后找不同的油田数量,这里靠在一起就是相同的油田,何为靠在一起就以一个油田为点,这个点的8个方向有油田的然后以这个8个方向中的任意一个油田为点如果这个点8个方向还有油田就还要算同一种类的油田,直到找不到才算另一种油田
这题思路是:弄个记录不同油田的变量sum=0,再先找个'@'点,sum加上1然后从这个点往8个方向(上下左右,左上,左下,右上,右下)广搜(即BFS),只要碰到'@'就把这个点变成'*',依次类推,最后输出sum就是不同油田数了,还是看AC代码吧
AC代码:
#include<iostream>
#include<queue>
const int MAX=101;
int fangxiang[8][2]={{-1,-1},{-1,0},{-1,1},{0,-1},{0,1},{1,-1},{1,0},{1,1}};
using namespace std;
typedef struct dian
{
	int x;
	int y;
};
char map[MAX][MAX];//油田的分布
int n;
int m;
void BFS(int x,int y)
{
	int i,j;
	queue<dian>que;
	dian in,out;
	in.x=x;
	in.y=y;
	que.push(in);
	while(!que.empty())
	{
		in=que.front();
		que.pop();
		dian next;
		for(i=0;i<8;i++)
		{
			next.x=out.x=in.x+fangxiang[i][0];
			next.y=out.y=in.y+fangxiang[i][1];
			if(next.x<1||next.x>n||next.y<1||next.y>m)
			continue;
			if(map[next.x][next.y]=='@')
			{
			 map[next.x][next.y]='*';
			 BFS(next.x,next.y);
			}
		}
	}
}
int main()
{
	int i,j,sum;
	while(cin>>n>>m,m)
	{
		sum=0;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				cin>>map[i][j];
			}
		}
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=m;j++)
			{
				if(map[i][j]=='@')
				{
					sum+=1;
					map[i][j]='*';
					BFS(i,j);
				}
			}
		}
		cout<<sum<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值