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. C#中的var

    var关键字是C#3.0开始新增的特性,称为推断类型(其实也就是弱化类型的定义). var a =23: 则a是整型,var a = “lgs”:则a是字符型,但使用Var类型要注意: 1.必须在定义 ...

  2. pdf can't copy text 无法复制文字

    有些 pdf 是通过图片弄出来的,或者被 protect 了. 我们会无法 copy 里面的字. 这个时候可以用 OCR (Optical character recognition) 就是从图片中识 ...

  3. JS获取系统时间--JavaScript基础

    1.网页中实时显示当前时间 <!DOCTYPE html><html lang="en"><head> <meta charset=&qu ...

  4. 【消息队列】kafka是如何保证高可用的

    一.kafka一个最基本的架构认识 由多个broker组成,每个broker就是一个节点:创建一个topic,这个topic可以划分为多个partition,每个partition可以存在于不同的br ...

  5. HDOJ-1156 Brownie Points II 线段树/树状数组(模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1156 在一张二位坐标系中,给定n个点的坐标,玩一个划线游戏(线必须穿过点),Stan先手画一条垂直的线,然后Ol ...

  6. CSS之transform-origin属性

    设置旋转元素的基点位置 语法 transform-orign:0 100% 0 transform-origin: x-axis y-axis z-axis; x-axis 定义视图被置于 X 轴的何 ...

  7. HashMap的两种排序方式

    Map<String, Integer> map = new HashMap<String, Integer>();map.put("d", 2);map. ...

  8. ajax被cancel问题(事件冒泡)

    发送ajax请求的时候发现ajax请求总是被cancel,但是请求却被执行了,查阅了知识之后,发现问题是:事件冒泡,记录下来,供自己和大家学习借鉴. 1. 前提,发出ajax的请求在form表单中 2 ...

  9. mysql命令查询

    含义 命令 查看gtid是否开启 show variables like '%gtid%';  查看只读信息 show global variables like "%read_only%& ...

  10. leetcode-algorithms-17 Letter Combinations of a Phone Number

    leetcode-algorithms-17 Letter Combinations of a Phone Number Given a string containing digits from 2 ...