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. HttpServletResponse实现文件下载

    import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...

  2. javascript:没有定义的变量和没有定义的属性

    1. 没有定义的变量 window.onload = function() { alert(a); // 报错: Uncaught ReferenceError: a is not defined / ...

  3. Python的安装以及编译器的安装

    首先要想写python语言,要安装并配置python的环境,点击python下载即可,当然需要看下自己电脑适合下载的版本,64位还是32位的即可. 安装一般情况安装在C盘即可,选择添加变量的配置,完成 ...

  4. JS设计模式——单例模式剖析

    转载于原文地址:https://blog.csdn.net/q1056843325/article/details/52933426 举一个通俗的例子,在页面中点击登录按钮,弹出了一个登录浮窗,这个登 ...

  5. VC++每个版本对应的库

    msvcp.msvcr60.71和80.dll,以及vcomp.dll(不带数字版本号)属于VC++2005版 msvcp.msvcr.vcomp90.dll属于 VC++2008版 msvcp.ms ...

  6. cf 1110 D

    哇真难啊,没注意到 可以开 dp[N][3][3]这种性质,也就是三个相同的顺子可以变成三个刻子,所以我们维护顺子的数目就不用超过三了,又因为每张牌i,只会被i-1,i-2,影响,所以额外开两维记录( ...

  7. oracle 字符转换成数字

    1>函数转换 select nvl2(translate(a.data, '\1234567890.', '\'), null, a.data) n, a.data from rpt_detai ...

  8. 9 ArcGIS Server 性能优化

    1.系统性能影响因子 地图.服务类型.数据源.客户端技术.CPU.数据结构.网络.内存.存储.部署.架构.服务接口.SDE等. 2.ArcGIS Server性能优化 数据结构与数据源:数据结构(矢量 ...

  9. PHP(Dom操作)

    jsDOM操作组成: ECMAscript:语法核心 BOM:浏览器对象模型 window:窗口 open close 定时器 dingsh history:历史记录 go back location ...

  10. Oracle工具——ADRCI

    ADRCI工具是Oracle11g才推出的新工具,主要用来管理alert文件.trace文件.dump文件.健康监事报告等. 这一篇简单介绍ADRCI工具. 用过11g的人都会发现,11g中alert ...