图--DFS求连通块
 
                              

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  and
 and  . 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.
. 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
这个题是DFS用法的模板题, 以上动图是种子填充(floodfill)的动态图, 也是DFS算法的搜索过程。
在这里给出上题代码:
#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()
{
//freopen( "in.txt", "r", stdin );
//freopen( "out.txt", "w", stdout );
while(scanf("%d%d", &m, &n)==&&m&&n)
{
for(int i=; i<m; i++)
scanf("%s", pic[i]);
memset(idx, , sizeof(idx));
int cnt = ;
for(int i=; i<m; i++)
for(int j=; j<n; j++)
if(idx[i][j]==&&pic[i][j]=='@')
dfs(i, j, ++cnt);
printf("%d\n", cnt);
}
return ;
}
图--DFS求连通块的更多相关文章
- DFS入门之二---DFS求连通块
		用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ... 
- UVA 572   Oil  Deposits油田(DFS求连通块)
		UVA 572 DFS(floodfill) 用DFS求连通块 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format: ... 
- [C++]油田(Oil Deposits)-用DFS求连通块
		[本博文非博主原创,均摘自:刘汝佳<算法竞赛入门经典>(第2版) 6.4 图] [程序代码根据书中思路,非独立实现] 例题6-12 油田(Oil Deposits,UVa572) 输入一个 ... 
- UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)
		UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ... 
- 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求连通块(种子填充)
		[问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ... 
- 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。
		这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ... 
- UVa 572 油田(DFS求连通块)
		https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ... 
随机推荐
- 161114、websocket实现心跳重连
			心跳重连缘由 在使用websocket过程中,可能会出现网络断开的情况,比如信号不好,或者网络临时性关闭,这时候websocket的连接已经断开, 而浏览器不会执行websocket 的 onclos ... 
- 160906、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
			互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这种情况下诞生的.现在核心业务抽取出来,作为独立的服务,使 ... 
- 在keil 4中添加stc系列芯片的方法--【sky原创】
			在keil 4中添加stc系列芯片的方法: 1.从官网下载uv3.cdb的文件网址是:http://www.stcmcu.com/ 2.下载好后把uv3.cdb文件改成STC.cdb:3. 然后将[S ... 
- Centos7 安装配置NFS
			一.安装 NFS 服务器所需的软件包 # yum install -y nfs-utils 二.编辑exports文件 # vim /etc/exports /data/disk1/video *(a ... 
- 3、JavaScript
			1. JavaScript简介 1.1. JavaScript由来 Netscape 发明了 JavaScript JavaScript由Netscape 在1995年发明.早期的主要目的是处理 ... 
- Windows下Python,setuptools,pip,virtualenv的安装
			Windows 2003平台,安装Python2.7.4,Python3.3,setuptools,pip,virtualenv. 安装Python2.7.4(当前版本是2.7.6),安装路径:C:\ ... 
- 第十二届浙江省大学生程序设计大赛-Beauty of Array                                                       分类:            比赛             2015-06-26 14:27    12人阅读    评论(0)    收藏
			Beauty of Array Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has an array A with N integers. ... 
- 分页sql
			/// <summary> /// 根据页数分页 /// </summary> /// <param name="page"></para ... 
- wpf的研究和反思
			WPF的研究和反思 目前是否适合使用wpf WPF(Windows Presentation Foundation)是微软推出的基于Windows Vista的用户界面框架,属于.NET F ... 
- Python学习笔记-Day1-Python基础
			1.python诞生 关于Python的起源,吉多·范罗苏姆在1996年写到: 六 年前,在1989年12月,我在寻找一门“课余”编程项目来打发圣诞节前后的时间.我的办公室会关门,但我有一台家用电脑, ... 
