深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态。

因此可以对所有的状态进行操作,或列举出所有的状态。


Lake Counting

POJ - 2386

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<string>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<cstdlib>
using namespace std;int m, n;
char garden[105][105];void dfs(int x, int y)
{//将当前点取消标记,避免重复查找garden[x][y] = '.';//遍历周围的八个点for (int dx = -1; dx <= 1; dx++){for (int dy = -1; dy <= 1;dy++){int nx = x + dx;int ny = y + dy;if(0<=nx && nx<n && 0<=ny && ny<m && garden[nx][ny]=='W')dfs(nx, ny);}}
}int main(void)
{while(~scanf("%d%d", &n,&m)){getchar();  //吸收两数字后的换行符memset(garden, 0, sizeof(garden));for (int i = 0; i < n;i++){for (int j = 0; j < m;j++)scanf("%c", &garden[i][j]);getchar();  //吸收每次输入一行后的换行符}int sum = 0;for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (garden[i][j] == 'W'){dfs(i, j);sum++;}}}cout << sum << endl;}return 0;
}

DFS:深入优先搜索 POJ-2386 Lake Counting相关推荐

  1. POJ 2386 Lake Counting [DFS]

    POJ 2386 Lake Counting 简单的DFS,用了stack代替递归,输入有问题,搞了蛮久,算法是没问题.所以以后一定要记得加上检查输入的那一步 然后对于点的定义以后就这么办吧 此外还有 ...

  2. poj 2386 Lake Counting

    poj   2386   Lake Counting                           题目链接:http://poj.org/problem?id=2386 题目大意:数湖. 题目 ...

  3. POJ 2386 Lake Counting

    链接:http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submis ...

  4. POJ 2386 Lake Counting DFS水水

    http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...

  5. DFS 之 poj 2386 Lake Counting

    // [11/1/2014 JmingS] /* 遍历整个图,找到 'W' 的点,对其周围八个点其进行深搜,若是 'W' 则用 '.' 替换. 最后,在遍历整个图的过程中,找到多少个 'W',即答案. ...

  6. POJ 2386(DFS)

    深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 举例说明之:下图是一个无向 ...

  7. Lake Counting POJ - 2386

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

  8. NUC1158 Lake Counting【DFS】

    Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...

  9. Openjudge1388 Lake Counting【DFS/Flood Fill】

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

最新文章

  1. Canvas之translate,scale,rotate,skew
  2. Beanutils-No origin bean specified问题分析
  3. ChannelInitializer 的添加
  4. rabbitmq java 应用实例
  5. # Schedulerx正式登陆Ali-k8s应用目录
  6. Mac中Python版本随意切换终极指南,简单到爆,什么都不用设置
  7. 更换Homebrew的更新源
  8. 基于8086的中断方式4*4矩阵键盘
  9. CentOS 系统及各版本介绍
  10. HDU-6437 Problem L.Videos(最大费用最大流拆点)
  11. 用dcloud平台的H5+实现消息推送APP端通知栏接收的问题
  12. dw怎么在框架中加入网页_DW中制作框架网页.ppt
  13. 显示隐藏文件夹,Mac显示.m2文件夹
  14. ewb文件用multisim打开_EWB的使用
  15. C语言程序设计编程题(六)15
  16. IOS 开发 手势使用
  17. 洛阳理工Linux实验报告,谁来给我抄实验报告啊!这一张又一张写得。。。。
  18. 【AD】原理图放置差分对,报错Missing Negative Net for differential pair
  19. “代理服务器出现问题,或者地址有误”解决方案
  20. 淘客心得nbsp;PV是王道(原创)

热门文章

  1. 我们试着用FBI的方法破解了一台安卓机
  2. 超过80%的自媒体月收入不足万元,2017年做自媒体还有赚钱的机会吗?
  3. 转 The connection to adb is down, and a severe error
  4. 使用 acl 库编写发送邮件的客户端程序
  5. Linux分区的认识
  6. 2021年中国工业互联网安全大赛核能行业赛道writeup之数据库登录
  7. 分布式账本(Distributed ledger)
  8. 100天59万行代码_如何抽出100天的代码时间
  9. 职称计算机word2007难吗,职称计算机word2007原题
  10. PAT(甲级)2018年冬季考试 7-2 Decode Registration Card of PAT