用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”。

我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次。比较典型的问题是”八连块问题“。即任意两格子所在位置相邻(上下左右对角共八个方位),则在一个连通块。典型例题:HDU 1241 Oil Deposits

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1241

题目描述:输入m行n列的字符矩阵, 统计字符“@”组成八连块的个数。

题意分析:读入数据 -> 通过二重循环遍历该图->遇到‘@’且没有被访问过->DFS其连通块并作标记->输出

代码(仅供参考):

#include <cstdio>
#include <cstring>
const int maxn = + ;
char a[maxn][maxn];
int n, m, idx[maxn][maxn]; void dfs(int r, int c, int id)
{
if(r < || r >= m || c < || c >= n) return ;
if(idx[r][c] > || a[r][c] != '@') return ;
idx[r][c] = id;
for(int i = -; i <= ; i++)
for(int j = -; j <= ; j++)
if(i != || j != ) dfs(r+i, c+j, id);
} int main()
{
while(~scanf("%d%d", &m, &n)){
if(n == && m == ) break;
for(int i = ; i < m; i++) scanf("%s", a[i]);
memset(idx, , sizeof(idx));
int cnt = ;
for(int i = ; i < m; i++)
for(int j = ; j < n; j++)
if(idx[i][j] == && a[i][j] == '@') dfs(i, j, ++cnt);
printf("%d\n", cnt);
}
return ;
}

DFS入门之二---DFS求连通块的更多相关文章

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

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

  2. UVA 572 Oil Deposits油田(DFS求连通块)

    UVA 572     DFS(floodfill)  用DFS求连通块 Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format: ...

  3. UVA 572 dfs求连通块

    The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...

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

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

  5. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  6. C. Edgy Trees Codeforces Round #548 (Div. 2) 并查集求连通块

    C. Edgy Trees time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. P1197 [JSOI2008]星球大战 [删边求连通块个数]

    展开 题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治着整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的 ...

  8. POJ-2386.Lakecounting(DFS求连通块)

    本题是一道连通块的入门题,用来练手,后续还会更新连通块的题目. 本题大意:一个n * m 的陆地上面有很多水洼,让你统计水洼的个数并输出. 本题思路:按照顺序遍历陆地,如果发现水洼就将它的八连块都进行 ...

  9. nyoj27-水池数目 (求连通块数目)【dfs】

    http://acm.nyist.net/JudgeOnline/problem.php?pid=27 水池数目 时间限制:3000 ms  |  内存限制:65535 KB 难度:4   描述 南阳 ...

随机推荐

  1. [Angular2 Router] Programmatic Router Navigation via the Router API - Relative And Absolute Router Navigation

    In this tutorial we are going to learn how to navigate programmatically (or imperatively) by using t ...

  2. ORCLE INNODB 博客与 innodb_lru_scan_depth

    https://blogs.oracle.com/mysqlinnodb/ http://mysqllover.com/?p=485 •MySQL. MySQL 5.6.10 http://www.m ...

  3. QStyle 新风格的实现

      摸索了很久,实际实现才发现很简单. 利用qt助手搜style可以发现style的实现和qapplication有关,在Qapplication里面搜到函数: void QApplication:: ...

  4. 微信js接口自定义分享内容

    最近客户有个要求,需要给网页添加微信分享功能,当然指的是用微信自带浏览器的时候,希望用户在最后一页点击分享的时候是分享的首页.曾经无意中看到过微信公众开发者平台提供了js接口,所以试着做了做,果然,跌 ...

  5. LeetCode45 Jump Game II

    题目: Given an array of non-negative integers, you are initially positioned at the first index of the ...

  6. 倒数计数器-CountDownLatch

    最近写一个多线程程序,老是MAIN方法执行完了子线程还没执行完(不知道以前怎么玩儿的),得不到最终结果,于是找到了CountDownLatch CountDownLatch是一个同步辅助类,java. ...

  7. CALayer实现点击屏幕放大或者缩小的一个圆

    #import "ViewController.h" #define WIDTH 50 @interface ViewController () @end @implementat ...

  8. 【转】ViewGroup的onMeasure和onLayout分析

    ViewGroup的onMeasure和onLayout分析 一个Viewgroup基本的继承类格式如下: 1 import android.content.Context; 2 import and ...

  9. 让UIScrollView、UITableView的滚动条一直显示

    先用xcode5.1.1或更低版本创建一个Category,如图: 然后拷贝以下代码到刚创建的UIImageView+ForScrollView.m文件中: - (void) setAlpha:(fl ...

  10. P2342 叠积木

    P2342 叠积木 17通过 66提交 题目提供者wwqk4444 标签树状数组线段树USACO 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 暂时没有讨论 题目背景 Cube Stacki ...