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. 第二百八十六天 how can I 坚持

    bug不断啊,头疼. 今天早上到的倒是挺早. 中午吃的黄焖鸡,晚上加了会班. 勇江的鱼都死了,杨建的还剩3条,晚上到家都快十点了,还洗了衣服,没捞出来呢, 希望可以请下来假吧. 晾上衣服睡觉.

  2. DBMS_ERRLOG记录DML错误日志(一)

    当一个DML运行的时候,如果遇到了错误,则这条语句会整个回滚,就好像没有执行过.不过对于一个大的DML而言,如果个别数据错误而导致整个语句的回滚,会浪费很多的资源和运行时间,从10g开始Oracle支 ...

  3. UVALive 7457 Discrete Logarithm Problem (暴力枚举)

    Discrete Logarithm Problem 题目链接: http://acm.hust.edu.cn/vjudge/contest/127401#problem/D Description ...

  4. [C语言 - 9] typedef

    关键字typedef   typedef 操作符可以看做是普通变量和类型之间的转换开关!! 例如 typedef int Integer;//定义了一种类型 int Integer;//定义了一个变量 ...

  5. 三星手机 Samsung Galaxy S3 无法复制粘贴的不完美解决方法

    问题简单描述 从上周开始我的Samsung Galaxy S3手机就无法实现复制粘贴功能了,每次复制时都提示复制到了剪贴板,但是粘贴时就会发现根本粘贴不了,无法打开剪贴板.真的是莫明其妙啊,我的手机没 ...

  6. Spring REST实践之HATEOAS

    HATEOAS HATEOAS(The Hypermedia As The Engine Of Application Statue)是REST架构的主要约束."hepermedia&quo ...

  7. c# 实现IComparable、IComparer接口、Comparer类的详解

    在默认情况下,对象的Equals(object o)方法(基类Object提供),是比较两个对象变量是否引用同一对象.我们要必须我自己的对象,必须自己定义对象比较方式.IComparable和ICom ...

  8. C# - DynamicObject with Dynamic

    本文转载:http://joe-bq-wang.iteye.com/blog/1872756 里面有动态Linq to xml的写法. There is a expando object which ...

  9. python的一些总结3

    好吧 刚刚的2篇文章都很水.. 这篇 也是继续水 在 templates 右键新建 html 文件:如 index.html (输入以下代码) <!DOCTYPE html> <ht ...

  10. QM课程01-功能概述

    QM模块满足一个 CIQ 系统的下列功能: 一般功能 · 在物料主记录中集成QM检验数据 · 管理供应商和客户或销售部门的物料相关的质量信息 · 把质量特性和物料说明中的检验特性连接 · 管理中央凭证 ...