Oil Deposits

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

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 <iostream>
#include <cstdio>
#include <queue>
using namespace std; const int SIZE = ;
char MAP[SIZE][SIZE];
int UPDATE[][] = {{-,},{,},{,-},{,},{-,-},{-,},{,-},{,}};
int N,M;
int ANS; struct Node
{
int x,y;
}QUE[SIZE * SIZE];
void dfs(int x,int y);
void bfs(int r,int c);
int main(void)
{
while(scanf("%d%d",&N,&M) && (N || M))
{
ANS = ;
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
scanf(" %c",&MAP[i][j]);
for(int i = ;i <= N;i ++)
for(int j = ;j <= M;j ++)
if(MAP[i][j] == '@')
{
ANS ++;
bfs(i,j);
}
printf("%d\n",ANS);
} return ;
} void dfs(int x,int y)
{
MAP[x][y] = '*';
int new_x,new_y;
for(int i = ;i < ;i ++)
{
new_x = x + UPDATE[i][];
new_y = y + UPDATE[i][];
if(new_x >= && new_x <= N && new_y >= && new_y <= M && MAP[new_x][new_y] == '@')
{
MAP[new_x][new_y] = '*';
dfs(new_x,new_y);
}
}
} void bfs(int r,int c)
{
MAP[r][c] = '*'; QUE[].x = r;
QUE[].y = c;
int front,rear;
front = ;
rear = ; while(front < rear)
{
int cur_x = QUE[front].x;
int cur_y = QUE[front].y;
front ++; for(int i = ;i < ;i ++)
{
int new_x = cur_x + UPDATE[i][];
int new_y = cur_y + UPDATE[i][];
if(new_x >= && new_x <= N && new_y >= && new_y <= M && MAP[new_x][new_y] == '@')
{
MAP[new_x][new_y] = '*';
QUE[rear].x = new_x;
QUE[rear].y = new_y;
rear ++;
}
}
}
}

HDU 1241 Oil Deposits (DFS/BFS)的更多相关文章

  1. HDOJ(HDU).1241 Oil Deposits(DFS)

    HDOJ(HDU).1241 Oil Deposits(DFS) [从零开始DFS(5)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  2. HDU 1241 Oil Deposits DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Oil Deposits Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  3. HDU 1241 Oil Deposits (DFS or BFS)

    链接 : Here! 思路 : 搜索判断连通块个数, 所以 $DFS$ 或则 $BFS$ 都行喽...., 首先记录一下整个地图中所有$Oil$的个数, 然后遍历整个地图, 从油田开始搜索它所能连通多 ...

  4. HDU 1241 Oil Deposits DFS搜索题

    题目大意:给你一个m*n的矩阵,里面有两种符号,一种是 @ 表示这个位置有油田,另一种是 * 表示这个位置没有油田,现在规定相邻的任意块油田只算一块油田,这里的相邻包括上下左右以及斜的的四个方向相邻的 ...

  5. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

  6. hdu 1241 Oil Deposits(DFS求连通块)

    HDU 1241  Oil Deposits L -DFS Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & ...

  7. DFS(连通块) HDU 1241 Oil Deposits

    题目传送门 /* DFS:油田问题,一道经典的DFS求连通块.当初的难题,现在看上去不过如此啊 */ /************************************************ ...

  8. HDU 1241 Oil Deposits(石油储藏)

    HDU 1241 Oil Deposits(石油储藏) 00 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)   Probl ...

  9. hdu 1241:Oil Deposits(DFS)

    Oil Deposits Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

随机推荐

  1. 30 分钟 Java Lambda 入门教程

    Lambda简介 Lambda作为函数式编程中的基础部分,在其他编程语言(例如:Scala)中早就广为使用,但在Java领域中发展较慢,直到java8,才开始支持Lambda. 抛开数学定义不看,直接 ...

  2. red5下nginx安装配置

    http://zfl110.iteye.com/blog/1155149 原址:http://lqw.iteye.com/blog/652763 安装Nginx 1.首先安装pcre-8.02.tar ...

  3. Sqoop 命令

    1)list-databases     List available databases on a server sqoop list-databases --connect jdbc:db2:// ...

  4. linux下面的查找命令

    在linux下面经常用查找命令,我自己最常用的是find whereis locate 关于find 我常用find的基本功能,如 find / -name filename 在某个目录下寻找文件. ...

  5. HTML5新增的CSS类API

  6. Object类、instanceof

    一.Object 1.所有类都默认继承至Object 2.两个常用的方法 2.1.toString:返回类的字符串描述,一般子类会重写用来打印属性 2.2.equals:默认比较两个对象的引用是否相同 ...

  7. Unity3D之UGUI学习笔记(一):UGUI介绍以及Canvas

    UGUI是Unity3D4.6官方提供的UI系统,支持2D和3D UI的开发. Unity3D UI史 OnGUI 在Unity4.6之前,官方提供的是OnGUI函数来开发UI界面,当然问题也比较多, ...

  8. velocity 快速入门

    基本语法      1.变量定义 : $name 注意 : a.名字和$配合一起用  b.更规范的写法是 ${name} 2.赋值 : #set($name = "威少") 3.条 ...

  9. npm package 装包匹配原则

    经常看到package.json 里面有这样的devDependencies: "devDependencies": { "@angular/common": ...

  10. mac下批量删除.svn文件

    mac下.svn是隐藏文件,而且即使我们调成可见的,一个一个删也很麻烦.今天正好同事问起来这个命令,于是想可能有些人也需要,于是还是放到博客里吧 命令比较简单,其实就是一条linux命令,打开终端,首 ...