HDU 1241  Oil Deposits

L -DFS

Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%I64d & %I64u

 

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
2

一道经典的dfs搜索题,搜索连通块个数

AC代码:

#include<cstdio>
#include<cstring>
const int maxn=+;
char pic[maxn][maxn];
int m,n,idx[maxn][maxn]; void dfs(int r,int c,int id)
{
if(r<||r>=m||c<||c>=n) //判定出界
return;
if(idx[r][c]>||pic[r][c]!='@') //不是'@'或者已经访问过的格子
return;
idx[r][c]=id; // 连通分量编号
for(int dr=-; dr<=; dr++)
for(int dc=-; dc<=; dc++)
if(dr!=||dc!=)
dfs(r+dr,c+dc,id);
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n)==&&m&&n)
{
for(i =; i<m; i++)
scanf("%s",pic[i]);
memset(idx,,sizeof(idx));
int q=;
for(i=; i<m; i++)
for(j=; j<n; j++)
if(idx[i][j]==&&pic[i][j]=='@')
dfs(i,j,++q);
printf("%d\n",q);
}
return ;
}
 

hdu 1241 Oil Deposits(DFS求连通块)的更多相关文章

  1. HDU1241 Oil Deposits —— DFS求连通块

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...

  2. UVa572 Oil Deposits DFS求连通块

      技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...

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

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

  4. HDU 1241 Oil Deposits (DFS or BFS)

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

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

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

  6. HDU 1241 Oil Deposits (DFS/BFS)

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

  7. HDU 1241 Oil Deposits DFS搜索题

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

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

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

  9. [C++]油田(Oil Deposits)-用DFS求连通块

    [本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ...

随机推荐

  1. 如何解决升级WIN服务的时候,不暴力停止服务 达到升级的目的

    同一个WIN服务,分别部署在A.B两台服务器上,前面使用netscaler负载均衡 ,A和B被请求频繁,几乎时时刻刻都被请求   .PS:发布WIN服务的正常流程是,停止WIN服务->发布WIN ...

  2. 《University Calculus》-chaper12-多元函数-拉格朗日乘数法

    求解条件极值的方法:拉格朗日乘数法 基于对多元函数极值方法的了解,再具体的问题中我们发现这样一个问题,在求解f(x,y,z)的极值的时候,我们需要极值点落在g(x,y,z)上这种对极值点有约束条件,通 ...

  3. 强迫症和拖延症患者如何应对马桶4(遨游Maxthon)“上次未关闭页面”丢失的问题

    强迫症和拖延症患者如何应对马桶4(遨游Maxthon)“上次未关闭页面”丢失的问题 用了马桶好多年,虽然一直bug不断,经常假死丢数据坑爹什么的,但是总得来说还是略有感情,不忍舍弃. 马桶一直有一个好 ...

  4. 亲测 安装 mysql5.5 64位

    1.选择 customn 更改安装目录2.选择 develop 模式,默认,占用内存较小. server machine 中等内存 dedicate mysql server machine 专用服务 ...

  5. 为什么app都是异步编程

    对本文题目首先需要了解一下什么是异步编程,异步编程即多线程编程. 多线程是一个比较轻量级的方法来实现单个应用程序内多个代码执行路径. 在具体理解多线程之前先看一个都理解的例子: 在系统级别内,程序并排 ...

  6. lvs+heartbeat搭建负载均衡高可用集群

    [172.25.48.1]vm1.example.com [172.25.48.4]vm4.example.com 集群依赖软件:

  7. Oracle 存储过程(2)

    http://www.cnblogs.com/chinafine/archive/2010/07/12/1776102.html http://blog.itpub.net/29485627/view ...

  8. win7/win8 64位系统注册TeeChart8.ocx 控件---以及dllregisterserver调用失败问题解决办法

    TeeChart控件就不多介绍了,很多朋友不知道开始怎么注册使用,尤其是在64位系统下如何注册的问题,具体如下: win7.win8  64位系统问题所在: 64位的系统一般都是可以安装32位程序的 ...

  9. (转)iFrame高度自适应

    第一种方法:代码简单,兼容性还可以,大家可以先测试下: function SetWinHeight(obj) { var win=obj; if (document.getElementById) { ...

  10. 移动页面缩放方法之(一)控制meta法

    <!DOCTYPE HTML> <html lang="zh-cn"> <head> <meta http-equiv="Con ...