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

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.

Source


 
  简单深搜
  遍历迷宫中所有的点,如果是‘W’,开始dfs搜索,将它临近的八个方向是‘W’的点全部走遍,并将走过的点变为‘.’,这样这一块‘W’区域就全部走完。一共走过了多少个这样的区域,就是结果。
  代码:
 #include <iostream>

 using namespace std;
int n,m;
char a[][];
int dx[] = {,,,,,-,-,-}; //八个方向
int dy[] = {,,,-,-,-,,};
bool judge(int x,int y)
{
if(x< || x>n || y< || y>m)
return ;
if(a[x][y]!='W')
return ;
return ;
}
void dfs(int x,int y)
{
a[x][y] = '.'; //将‘W’转化为‘.’
for(int i=;i<;i++){
int nx = x + dx[i];
int ny = y + dy[i];
//如果这一步是‘W’,且没有越界,可以走。
if(judge(nx,ny))
continue;
dfs(nx,ny);
}
}
int main()
{
while(cin>>n>>m){
int sum = ;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>a[i][j];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(a[i][j]=='W'){
sum++;
dfs(i,j);
}
cout<<sum<<endl;
}
return ;
}

Freecode : www.cnblogs.com/yym2013

poj 2386: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. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

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

  4. (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)

    题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...

  5. POJ 2386 Lake Counting (简单深搜)

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

  6. POJ 2386 Lake Counting(深搜)

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

  7. [POJ 2386] Lake Counting(DFS)

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

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

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

  9. POJ 2386 Lake Counting

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

随机推荐

  1. draw call 理解和优化

    draw call是openGL的描绘次数(directX没怎么研究,但原理应该差不多)一个简单的openGL的绘图次序是:设置颜色→绘图方式→顶点座标→绘制→结束.每帧都会重复以上的步骤.这就是一次 ...

  2. KindEditor 4.1.7的使用技巧

    1.载入进这个自己写的js代码 $(function(){ if ($("textarea.editor").length) { var editor = KindEditor.c ...

  3. css hacks

    /***** Selector Hacks ******/ /* IE6 and below */ * html #uno { color: red } /* IE7 */ *:first-child ...

  4. JS经验库

    1.IE状态栏无法结束的问题 function clearStatusBar(){ this.bd = document.body; this.tmp = document.creatElement( ...

  5. 【转载】XGBoost调参

    General Parameters: Guide the overall functioning Booster Parameters: Guide the individual booster ( ...

  6. mongodb - Master Slave Replication

    master-slave复制模式大多场景下都被replicat sets代替.官方也建议使用replicat sets. master-slave复制不支持自动failover. master-sla ...

  7. Unity3D-rigidBody.velocity

    还有半小时就下班了.写一下今天遇到的问题.处理方法以及一些自己的理解.理解的不一定对,还希望大家指正. 今天我做的效果是,hero的移动. 曾经做过用的是transform.Translate(Vec ...

  8. jquery遍历总结(转)

    遍历 DOM jQuery 提供了多种遍历 DOM 的方法. 遍历方法中最大的种类是树遍历(tree-traversal). 下一章会讲解如何在 DOM 树中向上.下以及同级移动. 向上遍历 DOM ...

  9. MySQL-group-replication 配置

    MySQL-Group-Replication 是mysql-5.7.17版本开发出来的新特性:它在master-slave 之间实现了强一致性, 但是就目前来说主要是性能不太好. [1]确定当前的m ...

  10. SignalR IOS Android

    http://www.dotblogs.com.tw/toysboy21/archive/2014/03/24/144505.aspx https://www.youtube.com/watch?v= ...