Lake Counting(POJ No.2386)

Description

Due to recent rains, water has pooled in various places in Farmer John's field, which is represented by a rectangle of N x M (1 <= N <= 100; 1 <= M <= 100) squares. Each square contains either water ('W') or dry land ('.'). Farmer John would like to figure out how many ponds have formed in his field. A pond is a connected set of squares with water in them, where a square is considered adjacent to all eight of its neighbors.

Given a diagram of Farmer John's field, determine how many ponds he has.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: M characters per line representing one row of Farmer John's field. Each character is either 'W' or '.'. The characters do not have spaces between them.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

  1. 10 12
  2. W........WW.
  3. .WWW.....WWW
  4. ....WW...WW.
  5. .........WW.
  6. .........W..
  7. ..W......W..
  8. .W.W.....WW.
  9. W.W.W.....W.
  10. .W.W......W.
  11. ..W.......W.

Sample Output

  1. 3
  1. 1 #include <iostream>
  2. 2 using namespace std;
  3. 3 int N,M;
  4. 4 //int res=0;
  5. 5 const int MAX_N=1000;
  6. 6 const int MAX_M=1000;
  7. 7 char field[MAX_N][MAX_M];
  8. 8 void dfs(int x,int y)
  9. 9 {
  10. 10 field[x][y]='.';
  11. 11 for(int dx=-1;dx<=1;dx++)
  12. 12 {
  13. 13 for(int dy=-1;dy<=1;dy++)
  14. 14 {
  15. 15 int nx=dx+x,ny=dy+y;
  16. 16 if(nx>=0&&nx<N&&ny>=0&&ny<M&&field[nx][ny]=='W')
  17. 17 dfs(nx,ny);
  18. 18 }
  19. 19 }
  20. 20 return;
  21. 21 }
  22. 22 void solve()
  23. 23 {
  24. 24 int res=0;
  25. 25 for(int i=0;i<N;i++) {
  26. 26 for(int j=0;j<M;j++){
  27. 27 if(field[i][j]=='W') {
  28. 28 dfs(i, j);
  29. 29 res++;
  30. 30 }
  31. 31 }
  32. 32 }
  33. 33 cout<<res<<endl;
  34. 34 }
  35. 35 int main() {
  36. 36 cin>>N>>M;
  37. 37 for(int x=0;x<N;x++)
  38. 38 {
  39. 39 for(int y=0;y<M;y++)
  40. 40 {
  41. 41 cin>>field[x][y];
  42. 42 }
  43. 43 // printf("\n");
  44. 44 }
  45. 45 solve();
  46. 46 //cout<<res<<endl;
  47. 47 return 0;
  48. 48 }

DFS----Lake Counting (poj 2386)的更多相关文章

  1. Lake Counting(poj 2386)

    题目描述: Description Due to recent rains, water has pooled in various places in Farmer John's field, wh ...

  2. DFS:Lake Counting(POJ 2386)

    好吧前几天一直没更新博客,主要是更新博客的确是要耗费一点精力 北大教你数水坑 最近更新博客可能就是一点旧的东西和一些水题,主要是最近对汇编感兴趣了嘻嘻嘻 这一题挺简单的,没什么难度,简单深搜 #inc ...

  3. Lake Counting (POJ No.2386)

    有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼? *** *W* *** /** *进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 ...

  4. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  5. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  6. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  7. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

  8. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  9. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

随机推荐

  1. echart 圆滑初始化化

    圆滑:主题下载对应主题js引入后注入对应名称参数方可使用主题 初始化:tab点击的时候初始化图表涉及到tab切换到的需要延迟加载否则默认宽度为100px 1.创建macarons.js文件 2.页面添 ...

  2. oracle.exe 内存占用过大

    现象: 明明各个schema 占用的磁盘空间都不大. oracle.exe 却占用了差不多 3G 的内存. 解决: 查了google,各种英文关键字没有找到原因. 最后,中文检索到了. https:/ ...

  3. HTML第五章总结

    A Chapter for <img> 前言 这一章讲了 Web 图片 format 的各自的优缺点和elements of 's attributes. Section1:Q1:How ...

  4. Vue.js的后端数据支持:使用Express建立app, 并使用MongoDB数据库。

    需要用到的backed tech stack: Node: JavaScript on the server/backend. That's basically what it is, but mor ...

  5. android -------- 错误Attribute application@allowBackup value=(true) from AndroidManifest.xml

    开发中遇到一个问题,运行项目时,出现了一个这如下这样的问题 问题: Manifest merger failed : Attribute application@allowBackup value=( ...

  6. android -------- 我创建的第一个 NDKDmeo 案例

    前面的NDK是弄的官方的,自己弄了一下,弄让他运行起来,今天来简单的写一个. 我是在Eclipse中开发的,创建一个NDKDemo项目,然后如下图: 在项目上–>右键–>Android T ...

  7. HTML页面加载完毕后运行的js

    Js方法:<script type=”text/javascript”> window.onload=function (){ var userName=”xiaoming”; alert ...

  8. Xpath做数据解析

    xpath是一个路径表达式, xpath学习 (1)xpath节点 在XPath中,有七种类型的节点:元素,属性,文本,命名空间,处理指令,注释以及文档节点:XML文档是被作为节点树来对待的.树的根被 ...

  9. 学习Spring Security OAuth认证(一)-授权码模式

    一.环境 spring boot+spring security+idea+maven+mybatis 主要是spring security 二.依赖 <dependency> <g ...

  10. SQL - 数据定义

    SQL 的数据定义功能包括模式定义.表定义.视图和索引的定义: 操作对象 操作方式 创建 删除 修改 模式  create schema drop schema   表  create table d ...