[C++]油田(Oil Deposits)-用DFS求连通块
[本博文非博主原创,均摘自:刘汝佳《算法竞赛入门经典》(第2版) 6.4 图]
[程序代码根据书中思路,非独立实现]
例题6-12 油田(Oil Deposits,UVa572)
输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块。如果两个字符“@”所在的格子相邻(横、纵或者对角线方向),就说它们属于一个八连块。例如,下图中有两个八连块。
一、分析
1.1 整体思路
图也有DFS和BFS遍历。由于DFS更容易编写,一般用DFS查找连通块:从每个"@"格子出发,递归遍历它周围的"@"格子。每次访问一个格子时,就给它写上一个"连通分量编号",这样就可以在访问之前检查它是否已经有了编号,从而避免同一个格子访问多次。
1.2 各坐标的邻接位置(3X3)扫描器

二、程序实现
//用DFS求连通块
#include<iostream>
#include<string.h>
using namespace std; const int maxn = ;
int cnt;
int *cntArray;
char matrix[maxn][maxn];
int idx[maxn][maxn];//标记数组
int m;
int n; void print(int rows, int cols){
for(int i=;i<rows;i++){
for(int j=;j<cols;j++){
printf("%d\t", idx[i][j]);
}
printf("\n");
}
} void dfs(int row,int col,int id){
if(row< || row>=m || col < || col >=n)
return;//边界
if((matrix[row][col] != '@') || (idx[row][col] > ))//已被标记或者不是@区域
return;
idx[row][col] = id;
for(int drow=-;drow<;drow++){//(邻接)扫描器
for(int dcol=-;dcol<;dcol++){
dfs(row + drow, col + dcol, id);
}
}
} int countIdx(){
cntArray = new int[cnt+];
memset(cntArray, , sizeof(cntArray));
int cursor = ;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
cursor = matrix[i][j];
cntArray[cursor]++;
}
}
} int main(){
while(scanf("%d %d", &m, &n) == && m && n){
for(int i=;i<m;i++){
scanf("%s", matrix[i]);
}
memset(idx, , sizeof(idx));
int cnt = ;
for(int i=;i<m;i++){
for(int j=;j<n;j++){
if(idx[i][j] == && matrix[i][j] == '@')
dfs(i, j, ++cnt);
}
}
print(m, n);
}
return ;
}
/*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
*/
效果图:

三、参考文献
1.刘汝佳.算法竞赛入门经典
[C++]油田(Oil Deposits)-用DFS求连通块的更多相关文章
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...
- hdu 1241 Oil Deposits(DFS求连通块)
HDU 1241 Oil Deposits L -DFS Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & ...
- ZOJ 1709 Oil Deposits(dfs,连通块个数)
Oil Deposits Time Limit: 2 Seconds Memory Limit: 65536 KB The GeoSurvComp geologic survey compa ...
- UVA 572 Oil Deposits油田(DFS求连通块)
UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ...
- HDU1241 Oil Deposits —— DFS求连通块
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1241 Oil Deposits Time Limit: 2000/1000 MS (Java/Othe ...
- UVA 572 dfs求连通块
The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSu ...
- DFS入门之二---DFS求连通块
用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...
- 用DFS求连通块(种子填充)
[问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...
- UVa572 Oil Deposits DFS求连通块
技巧:遍历8个方向 ; dr <= ; dr++) ; dc <= ; dc++) || dc != ) dfs(r+dr, c+dc, id); 我的解法: #include< ...
随机推荐
- Ubuntu16.04下的NetCore环境搭建(附录含Ubuntu 18.04 安装 NetCore2.1)
跨平台系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#linux VSCode安装:http://www.cnblogs.com/dunitia ...
- 从密码到token, 一个授权的故事 auth2.0
1 美好的旧时光 我经常怀念三十年前那美好的旧时光, 工作很轻松, 生活很悠闲. 上班的时候偶尔有些HTTP的请求发到我这里, 我简单的看一下, 取出相对应的html文档,图片,发回去就可以了, 然后 ...
- Git中撤销提交
Git的几种状态 未修改 原始内容 已修改 ↓ 工 作 区 已暂存 ↓ git add 暂 存 区 已提交 ↓ git commit 本地仓库 已推送 ↓ git push 远程仓库 注意:下面所有命 ...
- 详解Vue 开发模式下跨域问题
vue项目中,前端与后台进行数据请求或者提交的时候,如果后台没有设置跨域,前端本地调试代码的时候就会报“No 'Access-Control-Allow-Origin' header is prese ...
- jsp+servlet+poi导出数据库中的数据
index.jsp: <a href="POIout">简单导出数据</a> <a href="POIoutTemplate"&g ...
- mysql常用快速查询修改操作
mysql常用快速查询修改操作 一.查找并修改非innodb引擎为innodb引擎 # 通用操作 mysql> select concat('alter table ',table_schema ...
- python csv与字典操作
# encoding: utf-8 import csv d1 = {'banana':3,'apple':4,'pear':1,'orange':2} d2 = {'banana':3,'orang ...
- tomcat插件使用
1.pom.xml添加插件 <build> <plugins> <!-- tomcat7插件 --> <!-- 注意:目前来说,maven中央仓库还没有tom ...
- eclipse没有server选项
摘录自:https://www.cnblogs.com/xiaoxiaoweng/p/7298183.html 问题描述:eclipse没有server选项 解决问题:找到Help->Insta ...
- HDFS 开发中的文件配置优先级
一.先看集群上的配置,这里设置了文件块副本数为 3 上传一个文件试试 public class ConfigPriority { private Configuration conf; private ...
