Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 49414   Accepted: 24273

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

Hint

OUTPUT DETAILS:

There are three ponds: one in the upper left, one in the lower left,and one along the right side.

 #include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
char a[][];
int n,m;
void dfs(int x,int y)
{
a[x][y]='.';
for(int dx=-;dx<=;dx++){
for(int dy=-;dy<=;dy++){
int nx=x+dx,ny=y+dy;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='W'){
dfs(nx,ny);
}
}
}
return ;
}
void solve()
{
int res=;
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='W'){
dfs(i,j);
res++;
}
}
}
cout<<res<<endl;
}
int main()
{
while(cin>>n>>m){
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
}
return ;
}

自己再熟悉一遍:

 #include <iostream>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
int n,m;
char a[][];
int res;
int nx,ny;
void dfs(int x,int y)
{
//八个方向搜索
for(int i=-;i<=;i++){
for(int j=-;j<=;j++){
nx=x+i,ny=y+j;
if(nx>=&&nx<n&&ny>=&&ny<m&&a[nx][ny]=='W'){
a[nx][ny]='.';
dfs(nx,ny);
}
}
}
}
void solve()
{
res=;
//每次从发现W的时候开始搜索由于一簇只能算一个地方所以res计数只加一次
for(int i=;i<n;i++){
for(int j=;j<m;j++){
if(a[i][j]=='W'){
res++;
dfs(i,j);
}
}
}
}
int main()
{
while(cin>>n>>m){
memset(a,,sizeof(a));
for(int i=;i<n;i++){
for(int j=;j<m;j++){
cin>>a[i][j];
}
}
solve();
cout<<res<<endl;
}
return ;
}

Poj2386 Lake Counting (DFS)的更多相关文章

  1. POJ:2386 Lake Counting(dfs)

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

  2. POJ 2386——Lake Counting(DFS)

    链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...

  3. [USACO10OCT]Lake Counting(DFS)

    很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...

  4. Lake Counting(dfs)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

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

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

  6. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  7. poj-2386 lake counting(搜索题)

    Time limit1000 ms Memory limit65536 kB Due to recent rains, water has pooled in various places in Fa ...

  8. POJ2386 Lake Counting 【DFS】

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 D ...

  9. poj2386 Lake Counting(简单DFS)

    转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...

随机推荐

  1. 最新最全的Java面试题整理(内附答案)

    Java基础知识篇 面向对象和面向过程的区别 面向过程: 优点:性能比面向对象高,因为类调用时需要实例化,开销比较大,比较消耗资源;比如单片机.嵌入式开发.Linux/Unix等一般采用面向过程开发, ...

  2. 3 ansible-playbook 条件语句-外部变量使用

    外部变量指的是从playbook文件之外获取的数值 lookups file file是我们经常使用的一种lookups的方式,它的原理就是使用python的codecs.open打开文件然后把结果返 ...

  3. Kafka consumer poll(long)与poll(Duration)的区别

    最近在StackOverflow碰到的一个问题,即在consumer.poll之后assignment()返回为空的问题,如下面这段代码所示: consumer.subscribe(Arrays.as ...

  4. iOS - 提示用户升级版本并跳转到AppStore

    一.问题:自己做提示用户升级? 由于苹果做了自动升级,所有只要在应用程序中出现从AppStore检查版本更新,或者出现任何有关升级的提醒都会被拒,但是如果必须添加升级提示的话,可以配合后台通过添加AP ...

  5. Scala函数使用可变参数

    scala同java一样,在定义函数的时候支持接收可变长参数列表,即最后一个参数的可以被重复.示例代码如下: 结果: 在此代码中我们定义函数printInfo接收变长参数列表,其最后一个参数names ...

  6. 线程的信号量Semaphore

    线程和进程一样也有信号量,一个资源在同一时刻只有规定数量的线程能使用.

  7. 第四天,通过windows来执行第一个python文件步骤

    该看 第 38部分的啦

  8. Tanya and Candies

    Tanya and Candies time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  9. hdu 4544——消灭兔子

    游戏规则很简单,用箭杀死免子即可.  箭是一种消耗品,已知有M种不同类型的箭可以选择,并且每种箭都会对兔子造成伤害,对应的伤害值分别为Di(1 <= i <= M),每种箭需要一定的QQ币 ...

  10. java面试基础题------》Java 中List、Set、Map异同点

    借鉴地址:http://blog.csdn.net/speedme/article/details/22398395 几句喜欢的话,拷贝下来: 世间上本来没有集合,(只有数组参考C语言)但有人想要,所 ...