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中的泛型是什么?他们为什么在那里?他们是如何发展的?在学习基础知识时,对仿制药的透彻理解是非常重要的.因此,我阅读了<Jav ...

  2. 多级分类标签{dede:channelartlist}实现当前栏目颜色高亮显示

    我们知道,如果要在网站上多级分类显示的话,可以用下面的标签实现:   {dede:channelartlist typeid='4' cacheid='channelsonlist'}    < ...

  3. Linux 下执行Shell 脚本的方式

    Shell 脚本的执行方式通常有如下三种: (1)bash script-name 或者 sh script-name:(2)path/script-name或者./script-name:(3)so ...

  4. jstat命令详解

    Jstat是JDK自带的一个轻量级小工具.全称“Java Virtual Machine statistics monitoring tool”,它位于java的bin目录下,主要利用JVM内建的指令 ...

  5. windows 驱动开发 MDL 内核层 用户层共享内存

    参考资料 https://blog.csdn.net/wdykanq/article/details/7752909 http://blog.51cto.com/laokaddk/404584 内核层 ...

  6. C# 不安装Oracle客户端情况下,如何连接到Oracle数据库

    简介: 在我们开发应用场景经常碰到需要连接Oracle数据库,这也是相当常见的一种情况.一般.Net环境连接Oracle数据库,可以通过TNS/SQL.NET 配置文件,而 TNS 必须要 Oracl ...

  7. [原]Docker-issue(2) http: server gave HTTP response to HTTPS client

    系统环境 查看 文章末尾 附录 问题点:新建local registry后,push新的image到local registry  未能成功,并报错误: The push refers to repo ...

  8. django-registration

    快速开始指南 在安装django-registration之前,你需要先安装Django.django-registration 0.8需要Django1.1或更新版本的支持. Django进一步的信 ...

  9. python __setattr__、__getattr__、__getattribute__全面详解

    一.属性引用函数 hasattr(obj,name[,default])getattr(obj,name)setattr(obj,name,value)delattr(obj,name) 二.属性引用 ...

  10. 初识springcloud

    springcloud的基础是springboot,简单地说,就是通过写的springboot应用,使用springcloud集成. 在学习springcloud的过程中,自己的开发环境不能保证和博客 ...