题目描述:

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>
char map[][];
int n,m;
void dfs(int i,int j);
int main()
{
using namespace std;
//int n,m;
int sum = ;
cin >> n >> m;
//char map[n][m];
for(int i = ;i < n;i++)
for(int j = ;j < m;j++)
cin >> map[i][j];
for(int i = ;i < n;i++)
for(int j = ;j < m;j++)
{
if(map[i][j] == 'W')
{
dfs(i,j);
sum++;
}
}
cout << sum << endl;
return ;
} void dfs(int i,int j)
{
int x,y;
map[i][j] = '.';
for(int nx = -;nx <= ;nx++)
for(int ny = -;ny <= ;ny++)
{
x = i + nx;
y = j + ny;
if(x <= n && y <= m && x >= && y >= && map[x][y] == 'W')
dfs(x,y);
}
}

代码分析:

这道题用到了深度优先搜索法,对这个算法也是最近刚接触,所以可能说的不太好,所以恳请读者指正。

深度优先搜索法,通俗地讲,就是从初始状态,在一个方向上,一直访问到最后一个状态,然后再返回到前一个个状态,换个一个方向,继续访问到最后一个状态。

在这道题目上,我们从任意一个的'W'可以访问,看它的周围的八个状态,哪个状态是'W',就从这个状态再继续深入,直到某个状态周围的八个状态都不是'W',则返回前一个状态,直到这个方向上都不是'W',则这个方向的状态都访问完。在这里我们将'W'改为'.',表示访问过。

1次dfs后与初始的这个W连接的所以W就都被替换为'.',直到图中不再存在W为止,总共进行dfs的次数就是答案。

参考书籍:[挑战程序设计竞赛]

Lake Counting(poj 2386)的更多相关文章

  1. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

  2. Lake Counting (POJ No.2386)

    有一个大小为N*M的园子,雨后积起了水,八连通的积水被认为是链接在一起的求出园子里一共有多少水洼? *** *W* *** /** *进行深度优先搜索,从第一个W开始,将八个方向可以到达的 W修改为 ...

  3. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  4. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  5. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  6. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  7. POJ:2386 Lake Counting(dfs)

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

  8. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  9. POJ 之2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20003   Accepted: 10063 D ...

随机推荐

  1. centos 磁盘扩容,新建lv

    1,扩容已有lvm 上的lv 1.1 新建pv --> pvcreate /dev/sd* 1.2 把新增的pv添加到lvm -->vgextend vg_ruiy /dev/sd* 1. ...

  2. ListBox控件的操作与实现

    .NET FrameWork>參考>类库>System.Windows.Forms>ListBox类的属性 1. 属性列表:     SelectionMode     组件中 ...

  3. Git怎样同一时候删除多个仓下的同一个分支

    每次下载完代码我们都会在本地通过repo start my_local --all建立分支,这样我们下载的代码在每一个仓下都有一个名叫my_local的分支,有些时候我们因为须要还会建立其它分支,当我 ...

  4. JS闭包经典例题

    上一篇文章谈论了闭包的概念和一些应用,并给出一个例题,这篇文章就此道例题进行讨论. function fun(n,o) { console.log(o); return { fun:function( ...

  5. 2015.8.2 jdbc实现商品类的增删查改

    在惠普济宁基地进行了两周sql和java的学习,学到很多东西 刚才实现了用jdbc访问数据库对数据库进行操作,是用eclipse写的,过几天移植到NetBeans上,个人还是比较习惯看图形化界面 前几 ...

  6. 我用过的linux命令--安装JDK

    首先,我的测试环境是CentOS的linux虚拟机,如果想安装JDK,首先要有一个JDK.利用的软件就是WinSCP,把JDK从windows中传送到Linux中去. 1. JDK从Windows到L ...

  7. python自定义排序函数

    Python内置的 sorted()函数可对list进行排序: >>>sorted([36, 5, 12, 9, 21]) [5, 9, 12, 21, 36] 但 sorted() ...

  8. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  9. poj 2398 计算几何

    #include <iostream> #include<cstdio> #include<cstring> #include <algorithm> ...

  10. wampServer 修改mySql 的root用户密码

    刚安装好时  密码是空的  所以不用输入直接回车就能进入 修改密码参考http://www.cnblogs.com/hooray/archive/2011/07/23/2114792.htmlhttp ...