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 . 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

这个题是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求连通块的更多相关文章

  1. DFS入门之二---DFS求连通块

    用DFS求连通块也是比较典型的问题, 求多维数组连通块的过程也称为--“种子填充”. 我们给每次遍历过的连通块加上编号, 这样就可以避免一个格子访问多次.比较典型的问题是”八连块问题“.即任意两格子所 ...

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

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

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

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

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

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

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

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

  6. UVA 572 dfs求连通块

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

  7. 用DFS求连通块(种子填充)

    [问题] 输入一个m行n列的字符矩阵,统计字符“@”组成多少个八连块.如果两个字符“@”所在的格子相邻(横.竖或者对角线方向),就说它们属于同一个八连块.例如,图6-9中有两个八连块. 图6-9 [分 ...

  8. 图-用DFS求连通块- UVa 1103和用BFS求最短路-UVa816。

    这道题目甚长, 代码也是甚长, 但是思路却不是太难.然而有好多代码实现的细节, 确是十分的巧妙. 对代码阅读能力, 代码理解能力, 代码实现能力, 代码实现技巧, DFS方法都大有裨益, 敬请有兴趣者 ...

  9. UVa 572 油田(DFS求连通块)

    https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

随机推荐

  1. mysql+keepalived主从切换脚本 转

    Keepalived MySQL故障自动切换脚本   MySQL架构为master-slave(主从),master故障自动切换到slave上.当然也可以设置为双master,但这里有个弊端:就是当主 ...

  2. iOS中 项目开发易错知识点总结

    点击return取消textView 的响应者 - (BOOL)textFieldShouldReturn:(UITextField *)textField { [_contactTextFiled  ...

  3. Oracle PL/SQL之LOOP循环控制语句

    在PL/SQL中可以使用LOOP语句对数据进行循环处理,利用该语句可以循环执行指定的语句序列.常用的LOOP循环语句包含3种形式:基本的LOOP.WHILE...LOOP和FOR...LOOP. LO ...

  4. Word and MediaElement

    function jmc_new_lib(){// Because we still want the script to load but not the styleswp_enqueue_scri ...

  5. 处理字符串中的换行,将textarea中的带有换行的字符串变为逗号分隔的写法

    _setMultipleInputValues: function (param) { //Maybe need to modify here for the new parameter //add ...

  6. 【转】java URLConnection从网上下载图片或音乐

    try { //根据String形式创建一个URL对象,   URL url = new URL("http://www.baidu.com");   //实列一个URLconne ...

  7. ArrayList集合的实现原理

    一. ArrayList概述: ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境 ...

  8. EF查询分页

    static List<T> GetPageList(Func<T,bool> whereLambda,Func<T,object> orderLambda,int ...

  9. Matlab的libsvm的安装

    最关键的是compilers的选择(对于把Microsoft visual stdio 2005或者其他的编译器安装在自定义目录下的这一步非常关键)  以下是步骤:>> mex -setu ...

  10. 使用KNN对MNIST数据集进行实验

    由于KNN的计算量太大,还没有使用KD-tree进行优化,所以对于60000训练集,10000测试集的数据计算比较慢.这里只是想测试观察一下KNN的效果而已,不调参. K选择之前看过貌似最好不要超过2 ...