bfs找油田问题= =

原创 2012年03月23日 15:00:08

Problem L

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 35   Accepted Submission(s) : 10

Font: Times New Roman | Verdana | Georgia

Font Size:  

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


思路:果断bfs之,在以前写过的bfs代码上面稍作修改即可得到正确结果。


#include <stdio.h>
#include <string.h>
struct queuenode{								//建结构
	int a, b;
	}queue[100000];

int count, tx, ty, head = 0,tail = 0;			//定义
int a, b, n, temp;
int dir[8][2] = { {1,0} , {0,1} , {-1,0} , {0,-1} , {1,1} , {-1,-1} , {1,-1} , {-1,1} };
bool map[110][110];
int m, k, i, j;
int inarea(int x,int y)					//判断是否在区域内
{
	return x >= 0 && y >= 0 && x < a && y < b;
}

void bfs(  )									//bfs
{
	count++;
	tail = head;
	queue[head].a = i;							//初始化队列头部
	queue[head].b = j;
	map[i][j] = 1;
	while(head <= tail){	    				//在队列中有数据的情况下进行循环
		for(k = 0;k < 8; k++){				//八个方向了~!
			tx = queue[head].a + dir[k][0];
			ty = queue[head].b + dir[k][1];
			if(inarea(tx, ty) && map[tx][ty]== 0){
				tail++;
				queue[tail].a = tx;
				queue[tail].b = ty;
				map[tx][ty] = 1;
			}
		}
		head++;
	}
	return;
}
		
int main(void)									//主函数
{
	while(scanf("%d%d",&a,&b)==2&&a!=0){
		memset(map, 1, sizeof(map));
		while(scanf(" ")||scanf("\n")) ;
		for(i = 0;i < a; i++){					//扫描地图
			for(j = 0;j < b; j++){
				scanf("%c",&temp);
				if (temp == '*') map[i][j] = 1;
				else if (temp == '@') map[i][j] = 0;
				else {
					j--;
					continue;
				}
			}
		}
		
		count = 0;								//初始化计数
		for(i = 0;i < a;i ++){
			for(j = 0;j < b;j ++){
				if (map[i][j] == 0){			//从找到油田开始进入bfs
					bfs();
				}
			}
		}
		printf("%d\n",count);
	}
	return 0;
}
	
	



HDOJ 1241 油田 BFS(使用普通队列或循环队列) 2种方法

初级BFS算法 题目链接:点击打开链接 Oil Deposits Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3276...

杭电bfs 水题1241 Oil Deposits - 油田 详解 + 分析

文章 纯属原创 如有bug 还请高手指点
  • fat1995
  • fat1995
  • 2015年01月17日 21:05
  • 909

Mid-Central USA 1997--Oil Deposits (油田问题)

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu...
  • Pit3369
  • Pit3369
  • 2017年03月28日 21:32
  • 152

油田问题 UVA 572

Problem Description The GeoSurvComp geologic survey company is responsible for detecting undergro...
  • nenuhxs
  • nenuhxs
  • 2017年11月19日 11:32
  • 18

【HDU 1198】Farm Irrigation(字符串拼接/类油田问题/DFS)

Farm IrrigationTime HDU 1198 主要用深度优先搜索和拼接字符串来完成题目要求

ZOJ1709/POJ1562 油田问题/搜索/DFS

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油,并把这个大区域分成了很多小块。他们通过专业设备,来分析每个小块中是否蕴藏石油。如果这些蕴藏石...

双向BFS算法实现公交车行程问题

  • 2009年08月25日 11:49
  • 109KB
  • 下载

Oil Deposits——在图中找油田

Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting unde...

最优油田开发规划问题

  • 2008年08月30日 18:05
  • 129KB
  • 下载

最优油田开发规划问题

  • 2008年08月30日 17:30
  • 287KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:bfs找油田问题= =
举报原因:
原因补充:

(最多只允许输入30个字)