# Problem L

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

#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++;
map[i][j] = 1;
for(k = 0;k < 8; k++){				//八个方向了～！
if(inarea(tx, ty) && map[tx][ty]== 0){
tail++;
queue[tail].a = tx;
queue[tail].b = ty;
map[tx][ty] = 1;
}
}
}
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;
}



• 本文已收录于以下专栏：

## 搜索 BFS

• qq_35008554
• 2016年09月08日 16:22
• 238

## ACM油田问题

• a1291985595
• 2017年06月01日 14:07
• 990

## BFS最短路径问题新手快速入门

• yaoxunji
• 2017年05月10日 15:31
• 730

## 实现DFS之“油田”

“油田”问题是一个比较经典的体现DFS思想的题目，经过学习，对DFS也有了一点理解，下面介绍下这个题目~ 题目来源： Mid-Central USA 1997,ZOJ1709,POJ156...
• u012904198
• 2014年04月01日 22:57
• 2308

## POJ 3984 迷宫问题(BFS:迷宫最短路径且输出路径)

POJ 3984 迷宫问题(BFS:迷宫最短路径且输出路径) http://poj.org/problem?id=3984 分析: 典型的BFS应用,要你求从左上角到右下角的最短路径,且保证有唯一解,...
• u013480600
• 2014年05月10日 15:51
• 3138

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

GeoSurvComp地质调查公司负责探测地下石油储藏。 GeoSurvComp现在在一块矩形区域探测石油，并把这个大区域分成了很多小块。他们通过专业设备，来分析每个小块中是否蕴藏石油。如果这些蕴藏石...
• AgoniAngel
• 2015年07月25日 10:46
• 1411

## 用BFS找最短路，并打印路径

• jphaoren
• 2011年03月29日 13:11
• 3943

## bfs寻找增广路

• CRZbulabula
• 2015年10月05日 17:20
• 590

## 寻找连同块——油田问题 HDU 1241

The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu...
• qq_38851184
• 2017年07月20日 11:25
• 63

## BFS求解最短路经典算法

/*深度优先搜索隐式的使用栈进行计算,而宽度优先搜索采用了队列进行计算*/ #include #include #include using namespace std; typedef pairP...
• m0_37428263
• 2017年06月12日 13:52
• 266

举报原因： 您举报文章：bfs找油田问题= = 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)