HDU 1241 Oil Deposits

\[\color{blue}{Oil\;Deposits} \]

\[\color{green}{Time\;Limit: 2000/1000 MS (Java/Others)\quad Memory\;Limit: 65536/32768 K (Java/Others)} \]


\(\color{CornflowerBlue}{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.


\(\color{CornflowerBlue}{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 \leqslant m \leqslant100\) and \(1 \leqslant n \leqslant 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.


\(\color{CornflowerBlue}{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.


\(\color{CornflowerBlue}{Sample\;Input}\)

1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5 
****@
*@@*@
*@**@
@@@*@
@@**@
0 0 

\(\color{CornflowerBlue}{Sample\;Output}\)

0
1
2
2

\(\color{CornflowerBlue}{Source}\)

Mid-Central USA 1997


\(\color{CornflowerBlue}{Recommend}\)

Eddy | We have carefully selected several similar problems for you: 1010 1312 1242 1240 1181




题目描述

GeoSurvComp地质勘探公司负责地下石油矿床的探测。 GeoSurvComp每次都工作在一个大的矩形区域的土地,并创建一个网格,将土地划分为许多正方形地块。 然后,它分别分析每个地块,使用传感设备来确定该地块是否含有石油。 含有石油的地块称为油区。 如果两个油区是相邻的,那么它们是同一油矿床的一部分。 石油储量可能相当大,并且可能包含许多油区。 你的工作是确定一个网格中包含了多少不同的石油矿床。



输入

输入文件包含一个或多个网格。 每个网格以包含\(m\)\(n\)的行开始,网格中的行数和列数,用单个空格分隔。 如果\(m=0\),则表示输入结束;否则\(1 \leqslant m \leqslant100\)\(1 \leqslant n \leqslant 100\)。 下面是\(n\)个字符的\(m\)行(不计算行尾字符)。 每个字符对应一块土地,要么是'*'',代表没有油,要么是'@'',代表油区。



输出

对于每个网格,输出不同的石油矿床数量。,如果两个不同的油区是相邻的水平,垂直,或对角线,则它们是同一石油矿床的一部分。 一个石油矿床将不包含超过\(100\)油区。



PZ's solution

这题啊,没啥好说的,广搜模板题。

  • TAG:搜索;BFS广度优先搜索



PZ.cpp

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define N 105
int n,m,ans;
int fx[]={0,0,1,1,1,-1,-1,-1};
int fy[]={1,-1,1,-1,0,1,-1,0};
char mp[N][N];
bool vis[N][N];
queue<int>qx,qy;
void bfs(int x,int y){
	qx.push(x); qy.push(y); vis[x][y]=1;
	while(!qx.empty()){
		x=qx.front(); qx.pop();
		y=qy.front(); qy.pop();
		for(int i=0;i<8;++i){
			int nx=x+fx[i],ny=y+fy[i];
			if(1<=nx&&nx<=n&&1<=ny&&ny<=m){
				if(!vis[nx][ny]&&mp[nx][ny]=='@'){
					vis[nx][ny]=1;
					qx.push(nx); qy.push(ny);
				}
			}
		}
	}
}
int main(){
	while(scanf("%d %d",&n,&m) && m!=0){
		memset(vis,0,sizeof(vis));
		ans=0;
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j)
				cin>>mp[i][j];
		for(int i=1;i<=n;++i)
			for(int j=1;j<=m;++j)
				if(!vis[i][j]&&mp[i][j]=='@'){
					++ans;
					bfs(i,j);
				}
		printf("%d\n",ans);
	}
	return 0;
}
posted @ 2021-01-08 14:18  PotremZ  阅读(85)  评论(0编辑  收藏  举报