题目描述:

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. Genymotion配置及使用教程(最新最完整版附各部分下载地址)

    Genymotion配置及使用教程(最新最完整版附各部分下载地址) FROM:http://blog.csdn.net/beiminglei/article/details/13776013 早都听说 ...

  2. C++中引用

    在C语言中&这个符号表示了取地址符,但是在C++中它却有着不同的用途,掌握C++的&符号,是提高代码执行效率和增强代码质量的一个很好的办法.一.引用简介 引用就是某一变量(目标)的一个 ...

  3. LOJ 1341 Aladdin and the Flying Carpet(质因子分解)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1341 题意:给两个数a,b,求满足c * d = a且c>=b且d>=b的 ...

  4. BZOJ 1649: [Usaco2006 Dec]Cow Roller Coaster( dp )

    有点类似背包 , 就是那样子搞... --------------------------------------------------------------------------------- ...

  5. Latex调整行距

    修改行间距的方法: \usepackage{setspace}%使用间距宏包 \begin{document} \begin{spacing}{2.0}%%行间距变为double-space 双倍行距 ...

  6. elasearch 版本控制

    http://192.168.32.81:9200/library/books/8/ GET { "_index": "library", "_typ ...

  7. BZOJ 1005 明明的烦恼 (组合数学)

    题解:n为树的节点数,d[ ]为各节点的度数,m为无限制度数的节点数. 则               所以要求在n-2大小的数组中插入tot各序号,共有种插法: 在tot各序号排列中,插第一个节点的 ...

  8. BZOJ 1023

    program bzoj1023; uses math; ; maxn=; maxm=; type edge=record togo,next:longint; end; var n,m,cnt,in ...

  9. 2014年去哪儿网笔试题--有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来。

    有两个文件context.txt和words.conf,请尝试将他们合并成为一段文字,并打印出来. 这两个文件内容如下: context.txt “并不是每个人都需要$(qunar)自己的粮食,$(f ...

  10. Warning: Unable to send packet: Error with PF_PACKET send() [11]: Message too long (errno = 90)

    今天在使用tcpreplay重放流量时,发现有的数据包没有发送成功: Warning: Unable to send packet: Error with PF_PACKET send() [215] ...