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 1
  2. *
  3. 3 5
  4. *@*@*
  5. **@**
  6. *@*@*
  7. 1 8
  8. @@****@*
  9. 5 5
  10. ****@
  11. *@@*@
  12. *@**@
  13. @@@*@
  14. @@**@
  15. 0 0

Sample Output

  1. 0
  2. 1
  3. 2
  4. 2
  5.  
  6. 解题思路:这是一个简单的DFS问题,就是一个m*n的矩形里面有多少块封闭的@区间,用深搜的方法即可
    程序代码:
  1. #include <cstdio>
  2. using namespace std;
  3. int c[][]={{,},{,},{,},{-,},{-,},{-,-},{,-},{,-}};
  4. char a[][];
  5. int m,n,d;
  6. void dfs(int i,int j)
  7. {
  8.  
  9. a[i][j]='*';
  10. for(int k=;k<;k++)
  11. {
  12. int x=i+c[k][];
  13. int y=j+c[k][];
  14. if(a[x][y]=='@'&&x>=&&x<=m&&y>=&&y<=n)
  15. dfs(x,y);
  16. }
  17. return ;
  18. }
  19.  
  20. int main()
  21. {
  22. while(scanf("%d%d",&m,&n)==&&m&&n)
  23. {
  24. d=;
  25. int i,j;
  26. for( i=;i<m;i++)
  27. scanf("%s",a[i]);
  28. for( i=;i<m;i++)
  29. for( j=;j<n;j++)
  30. if(a[i][j]=='@')
  31. { d++;
  32. dfs(i,j);
  33. }
  34. printf("%d\n",d);
  35. }
  36. return ;
  37. }
  1.  

暴力求解——UVA 572(简单的dfs)的更多相关文章

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

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

  2. UVa 572 Oil Deposits(DFS)

     Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil ...

  3. POJ 1562(L - 暴力求解、DFS)

    油田问题(L - 暴力求解.DFS) Description The GeoSurvComp geologic survey company is responsible for detecting ...

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

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

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

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

  6. UVA.548 Tree(二叉树 DFS)

    UVA.548 Tree(二叉树 DFS) 题意分析 给出一棵树的中序遍历和后序遍历,从所有叶子节点中找到一个使得其到根节点的权值最小.若有多个,输出叶子节点本身权值小的那个节点. 先递归建树,然后D ...

  7. 2018 Spring Single Training B (uva 572,HihoCoder 1632,POJ 2387,POJ 2236,UVA 10054,HDU 2141)

    这场比赛可以说是灰常的水了,涨信心场?? 今下午义务劳动,去拿着锄头发了将近一小时呆,发现自己实在是干不了什么,就跑到实验室打比赛了~ 之前的比赛补题补了这么久连一场完整的都没补完,结果这场比完后一小 ...

  8. BestCoder Round #79 (div.2)-jrMz and angles,,暴力求解~

    jrMz and angle       Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/65536 K (Java/Other ...

  9. 逆向暴力求解 538.D Weird Chess

    11.12.2018 逆向暴力求解 538.D Weird Chess New Point: 没有读好题 越界的情况无法判断,所以输出任何一种就可以 所以他给你的样例输出完全是误导 输出还搞错了~ 输 ...

随机推荐

  1. HTML5+移动APP(2)

    原理: html 页面负责内容: ui 负责页面样式: js 负责调用原生app方法. html5: html5这部分负责页面,也就是app中你看到的东西,大概的架构和内容 ui: mui 介绍:和H ...

  2. app图标和启动页设置

    弄了一下午,终于把iOS中图标的设置和启动页的设置弄明白了.我想以后再也不会浑了. 进入正题: 一:apple 1).iPhone4s 3.5寸屏,也就是640*960,但在模拟器上正常用的是320* ...

  3. UIAlertController (UIActionSheet, UIAlertView is deprecated in iOS 8.)

    iOS 8 后 UIAlertView 和  UIActionSheet 都被合并到了 UIAlertController里面. 文档原文: Important: UIAlertView is dep ...

  4. Java反射学习(java reflect)(二)

    ok之前说了Java的反射和反射分析类,那这些东西有神马作用呢,下面就来说应用: 三.运行时使用反射分析对象 简单写一个Employee类,然后利用JAVA反射去取name域,getDeclareFi ...

  5. this的一个作用 当前对象

    class Person{ String name="小花"; int age=19; void eat(){  System.out.println("在吃饭" ...

  6. 哈哈,CSDN又支持Windows Live Writer了

    从10年开始写CSDN博客,后面不支持WLW了,就不怎么写了,话说自带的编辑器确实不怎么样,不过又支持了,那就哈哈,重新开工了. 关于如何配置的,跟以前一样,详情如下所示: http://blog.c ...

  7. 初试ubuntu14.4问题集锦

    接触Linux系统也好长时间了,但每次都是浅尝则止.前几天突发兴趣,想认真地学习一下这个名扬天下的稳定的操作系统.于是试着装了一下炒作的最凶的Ubuntu. 安装的Ubuntu系统版本为14.04. ...

  8. iOS 获取项目名称及版本号

    可用于版本让用户手动检测是否有版本更新可用. NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];  CFSho ...

  9. 分享29个超赞的响应式Web设计

    原文自:http://www.csdn.net/article/2013-01-16/2813678-responsive-design-websites 最近几年,响应式Web设计不断印入人们眼帘, ...

  10. C语言-01基础语法

    1)         总结常见文件的拓展名 .c 是C语言源文件,在编写代码的时候创建 .o 是目标文件,在编译成功的时候产生 .out 是可执行文件,在链接成功的时候产生 2)         总结 ...