转载请注明出处:

viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents

题目链接:http://poj.org/problem?id=1562

----------------------------------------------------------------------------------------------------------------------------------------------------------
欢迎光临天资小屋http://user.qzone.qq.com/593830943/main

----------------------------------------------------------------------------------------------------------------------------------------------------------

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

代码例如以下:

#include <iostream>
#include <algorithm>
using namespace std;
#include <cstring>
#define TM 100+17
int N, M;
char map[TM][TM];
bool vis[TM][TM];
int xx[8]={0,1,1,1,0,-1,-1,-1};
int yy[8]={1,1,0,-1,-1,-1,0,1};
void DFS(int x, int y)
{
vis[x][y] = true;
for(int i = 0; i < 8; i++)
{
int dx = x+xx[i];
int dy = y+yy[i];
if(dx>=0&&dx<N&&dy>=0&&dy<M&&!vis[dx][dy]&&map[dx][dy] == 'W')
{
vis[dx][dy] = true;
DFS(dx,dy);
}
}
}
int main()
{
int i, j;
while(cin>>N>>M)
{
int count = 0;
memset(vis,false,sizeof(vis));
for(i = 0; i< N; i++)
{
cin>>map[i];
}
for(i = 0; i < N; i++)
{
for(j = 0; j < M; j++)
{
if(map[i][j] == 'W' && !vis[i][j])
{
count++;
DFS(i,j);
}
}
}
cout<<count<<endl;
}
return 0;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

poj2386 Lake Counting(简单DFS)的更多相关文章

  1. Poj2386 Lake Counting (DFS)

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

  2. POJ2386 Lake Counting 【DFS】

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

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

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

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

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

  5. POJ:2386 Lake Counting(dfs)

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

  6. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

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

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

  8. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

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

  9. POJ 2386——Lake Counting(DFS)

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

随机推荐

  1. 【C++】动态开辟二维数组

    二维数组在内存中的分配例如以下: C方式呈现: <span style="font-size:18px;"> #include <iostream> usi ...

  2. TextView中如何支持html标签,放置图片和动作标签

    TextView文本框和输入框几乎是一个正常的带界面的可交互的Android应用的基本组成 TextView主要作用是显示文本内容,其实还可以显示图片,当然有必要的话还可以为文本内容添加动作相应用户的 ...

  3. How to debug with IntelliJ IDEA + Grails 2.3.x (转)

    问题: 最近访问grails.org,看到grails framework已经发展到2.3.x了,不免想尝尝鲜.下载了最新的grails-2.3.x之后,创建了一个新的grails app. 添加Bo ...

  4. java-IO操作性能对照

    在软件系统中.IO速度比内存速度慢,IO读写在非常多情况下会是系统的瓶颈. 在java标准IO操作中,InputStream和OutputStream提供基于流的IO操作.以字节为处理单位:Reade ...

  5. B桥接模式ridge

    1.一个简短的引论 1)模式概述:将抽象部分与实现部分分离.使它们都能够独立的变化.让抽象类和派生类各自实现自己的对象.当一个系统有多维度的变化时,将各个维度分离出来让它们独立于变化(多角度地分类实现 ...

  6. WPF换肤之一:创建圆角窗体

    原文:WPF换肤之一:创建圆角窗体 我们都期望自己的软件能够有一套看上去很吸引人眼球的外衣,使得别人看上去既专业又有美感.这个系列就带领着大家一步一步的讲解如何设计出一套自己的WPF的窗体皮肤,如果文 ...

  7. Android四个存储数据的SharedPreferences

    除了SQLite外部数据库.SharedPreferences它是一个轻量级的数据存储,其本质是基于XML文件存储key-value键值数据,通过定期用它来存储一些简单的配置信息.它的存储位置/dat ...

  8. Google是不是真的不能用了?非常奇怪的问题

    这几天,事实上是这一阵子. 我连用goagent都无法上google了. 可怜我一直用Gmail邮箱.但如今我连用代理都上不了Gmail了. .. 是我自己电脑本身的问题吗?非常奇怪的问题,我原先用g ...

  9. 上delloc 无呼叫 故障排除 笔记

    经验 delloc 无呼叫 基本上可以得出结论,即循环引用的原因. 遇到这样的情况基本上可分为 1: 属性声明weak的地方 写成了 strong  .比方delegate. 2: block语法块中 ...

  10. hibernate操作数据库总结

    这篇文章用于总结hibernate操作数据库的各种方法 一.query方式 1.hibernate使用原生态的sql语句执行数据库查询 有些时候有些开发人员总觉得用hql语句不踏实,程序出现了错误,就 ...