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

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

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

    (1)边长的计算公式是:  output_h =(originalSize_h+padding*2-kernelSize_h)/stride +1 输入图片大小为200×200,依次经过一层卷积(ke ...

  2. [Spring] 关联类和bean | autowire=byName|byType

    ref:https://www.tutorialspoint.com/spring/spring_autowiring_byname.htm project:Working Set: Spring&g ...

  3. mysqlsh : mysql shell tutorial

    MySQL Shell 是一个高级的命令行客户端以及代码编辑器for Mysql. 除了SQL,MySQL Shell也提供脚本能力 for JS and Python. When MySQL she ...

  4. c++-pimer-plus-6th-chapter03

    Chapter Review Having more than one integer type lets you choose the type that is best suited to a p ...

  5. LeetCode--415--字符串相加

    问题描述: 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和. 注意: num1 和num2 的长度都小于 5100. num1 和num2 都只包含数字 0-9. num1 和nu ...

  6. 20181013xlVba计算优秀率及合格率

    Sub 计算高一优秀合格率() Dim Wb As Workbook Dim Sht As Worksheet Dim oSht As Worksheet Dim dOs As Object 'Out ...

  7. yarn基础解释

    https://yarnpkg.com/zh-Hans/docs Yarn 对你的代码来说是一个包管理器, 你可以通过它使用全世界开发者的代码,或者分享自己的代码. 代码通过包(package)(或者 ...

  8. 完整的Django入门指南学习笔记2

    part2: 前沿 在第一节中,我们安装了项目所需要的一切:Python3.6以及在虚拟环境中运行的Django2.0,这部分教程继续在项目上编写代码. 开始写代码前,先讨论下项目的相关背景知识,然后 ...

  9. 初次安装git配置用户名和邮箱

    初次安装git配置用户名和邮箱 初次安装git需要配置用户名和邮箱,否则git会提示:please tell me who you are. 你需要运行命令来配置你的用户名和邮箱: $ git con ...

  10. Node.js + Express中间件详解

    使用中间件 Express是一种路由和中间件Web框架,它具有自己的最小功能:Express应用程序本质上是一系列中间件函数调用. 中间件函数是可以访问请求对象 (req),响应对象(res)以及应用 ...