POJ 1562-Oil Deposits(BFS)

Oil Deposits

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12621   Accepted: 6888

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 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

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
遇到@就搜(广搜深搜都可以)8个方向把相邻的@清掉
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct node
{
	int x,y;
	node (int a,int b){x=a;y=b;}
};
int mv[8][2]={{0, 1}, {0, -1}, {1, 0}, { -1, 0},{-1,-1},{-1,1},{1,-1},{1,1}};
char ma[110][110];
int m, n, ans;
void bfs(int x,int y)
{
	ma[x][y]='*';
	queue <node> Q;
	Q.push(node(x,y));
	while(!Q.empty())
	{
		node v=Q.front();Q.pop();
		if(ma[v.x][v.y]=='@')
			ma[v.x][v.y]='*';
		for(int i=0;i<8;i++)
		{
			int tx=v.x+mv[i][0];
			int ty=v.y+mv[i][1];
			if(tx>=0&&tx<m&&ty>=0&&ty<n&&ma[tx][ty]=='@')
			{
				ma[tx][ty]='*';
				Q.push(node(tx,ty));
			}
		}
	}
}
int main()
{
	int i;
	while (cin >> m >> n)
	{
		if (!m && !n)
		{
			break;
		}
		getchar();
		for (i = 0; i < m; i++)
		{
			cin >> ma[i];
		}
		ans=0;
		for(int i=0;i<m;i++)
			for(int j=0;j<n;j++)
			if(ma[i][j]=='@')
		    {
			  bfs(i,j);
			  ans++;
		    }
		cout<<ans<<endl;
	}

	return 0;
}


POJ 1562-Oil Deposits(BFS)

时间: 2024-08-04 12:00:29

POJ 1562-Oil Deposits(BFS)的相关文章

UVA Oil Deposits (BFS)

 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.

(简单) POJ 1562 Oil Deposits,BFS。

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

[POJ] 1562 Oil Deposits (DFS)

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16655   Accepted: 8917 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

poj 1562 Oil Deposits

Oil Deposits Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 13505   Accepted: 7345 Description The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular r

Oil Deposits(油田)

题目: The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvCompworkswithonelargerectangularregionoflandatatime,andcreatesagridthatdivides the land into numerous square plots. It then analyzes each plot s

poj 3669 Meteor Shower(bfs)

Description Bessie hears that an extraordinary meteor shower is coming; reports say that these meteors will crash into earth and destroy anything they hit. Anxious for her safety, she vows to find her way to a safe location (one that is never destroy

poj 2251 Dungeon Master(bfs)

题目链接  http://poj.org/problem?id=2251 题意:一个立体空间, 输入三个数,L,R,C,代表有L个平面,R行,C列,.代表可以走,#代表不能走,S代表开始点,E代表结束点,问从S开始走,对每个位置,有六个走法,即空间的六个方向的走法(上下东南西北),一分钟可以走一个点,问从S走到E点,最少可以经过多少分钟,若不能到达,则输出Trapped! 简单的三维bfs随便做一下就可以了. #include <iostream> #include <cstring&g

POJ 2435Navigating the City(bfs)

题意:给你一个地图,’+’代表十字路口,‘-’‘|’表示街道,‘.’表示建筑物,‘s’,’E’ 起点和终点.输出从起点到终点的的 最短路径(包括方向和沿该方向的经过的十字路口数) 分析:ans[i][j],起点到(i,j)点的最短路径,bfs求出所有,再从终点回推到起点得最短路径. #include <map> #include <set> #include <list> #include <cmath> #include <queue> #in

题目--oil Deposits(油田) 基础DFS(深度搜索)

上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--depth first search,话不多说,直接上题了解: Description:某石油勘探公司正在按计划勘探地下油田资源,工作在一片长方形的地域中.他们首先将该地域划分为许多小正方形区域,然后使用探测设备分别探测每一块小正方形区域内是否有油.若在一块小正方形区域中探测到有油,则标记为’@’,否则标