原题链接
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

用DFS来做,从有‘W’的地方,将当前位置变成‘.’,把当前d循环遍历8个方向,直到没有W为止

#include <iostream>
#include <cstdio>
using namespace std;
const int N = 120;
char g[N][N];//存储
int n,m;
void dfs(int x,int y){g[x][y] = '.';//将当前位置置为‘.’//循环遍历八个方向for(int dx = -1;dx<=1;dx++){dx//for(int dy = -1;dy<=1;dy++){int nx = x + dx, ny = y + dy;//更新一下//判断当前位置是否越界,是否是W可以继续,如果可以就继续调用if(nx>=0 && nx<=n && ny>=0 && ny<m && g[nx][ny] == 'W')                       dfs(nx,ny);}}return ;
}void solve()
{int res=0;for(int i=0;i<n;i++){for(int j=0;j<m;j++){if(g[i][j]=='W') dfs(i,j),res++;}}printf("%d\n",res);
}
int main()
{std::ios::sync_with_stdio(false);cin.tie(NULL);cin>>n>>m;for(int i=0;i<n;i++){for(int j=0;j<m;j++){cin>>g[i][j];}}solve();return 0;
}

POJ-2386--Lake Counting相关推荐

  1. poj 2386 Lake Counting

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

  2. POJ 2386 Lake Counting [DFS]

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

  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. DFS:深入优先搜索 POJ-2386 Lake Counting

    深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...

  9. 信息学奥赛一本通(1249:Lake Counting)

    1249:Lake Counting 时间限制: 1000 ms         内存限制: 65536 KB 提交数: 9435     通过数: 4902 [题目描述] 题意:有一块N×M的土地, ...

  10. NUC1158 Lake Counting【DFS】

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

最新文章

  1. mysql数据库基本介绍及常见使用
  2. Mask-RCNN论文解读
  3. Android 更新项目名称
  4. poj3669 Meteor Shower(预处理+bfs)
  5. VS关闭却不关闭IIS Express并利用其进行调试
  6. OverflowError: Python int too large to convert to C long
  7. Vant(有赞)UI框架爬坑记——图片轮播+预览
  8. 2020中兴捧月算法大赛 埃德加考特派 区域优胜奖源代码
  9. 信息学奥赛一本通:1097:画矩形
  10. 一个有趣的模型组合预测模型
  11. php edm 系统,edm.php · 那些年我们一起/fanwe - Gitee.com
  12. 论文浏览(45) MiCT: Mixed 3D/2D Convolutional Tube for Human Action Recognition
  13. 联邦学习首次被纳入Gartner隐私计算技术成熟度曲线
  14. OSChina 周一乱弹 ——女人比代码复杂多了,搞不懂!
  15. uniapp写微信授权登录
  16. 《恋上数据结构与算法》第1季:算法概述
  17. JavaScript 中 mouseenter 与mouseover 区别
  18. 中国地理知识大全(转载)
  19. LockSupport的park和unpark的原理
  20. php设计模式:单例模式

热门文章

  1. 数据操作语言:连接查询-外连接
  2. 《游戏学习》 Java实现潜艇大战游戏源码
  3. 洛谷P1115最大子段和题解
  4. 段公子 Facebook
  5. 几个朋友合伙创业,如何分配股权?
  6. 有刷直流电机H桥功率驱动电路的设计心得
  7. Android厂商推送冲突了。。,三级缓存对性能的影响
  8. 使用JS实现简单的日历插件
  9. gettimeofday/clock_gettime
  10. COM组件的接口和对象