POJ-2386--Lake Counting
原题链接
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.
OutputLine 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相关推荐
- poj 2386 Lake Counting
poj 2386 Lake Counting 题目链接:http://poj.org/problem?id=2386 题目大意:数湖. 题目 ...
- POJ 2386 Lake Counting [DFS]
POJ 2386 Lake Counting 简单的DFS,用了stack代替递归,输入有问题,搞了蛮久,算法是没问题.所以以后一定要记得加上检查输入的那一步 然后对于点的定义以后就这么办吧 此外还有 ...
- POJ 2386 Lake Counting
链接:http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- POJ 2386 Lake Counting DFS水水
http://poj.org/problem?id=2386 题目大意: 有一个大小为N*M的园子,雨后积起了水.八连通的积水被认为是连接在一起的.请求出院子里共有多少水洼? 思路: 水题~直接DFS ...
- DFS 之 poj 2386 Lake Counting
// [11/1/2014 JmingS] /* 遍历整个图,找到 'W' 的点,对其周围八个点其进行深搜,若是 'W' 则用 '.' 替换. 最后,在遍历整个图的过程中,找到多少个 'W',即答案. ...
- POJ 2386(DFS)
深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 举例说明之:下图是一个无向 ...
- Lake Counting POJ - 2386
Due to recent rains, water has pooled in various places in Farmer John's field, which is represented ...
- DFS:深入优先搜索 POJ-2386 Lake Counting
深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...
- 信息学奥赛一本通(1249:Lake Counting)
1249:Lake Counting 时间限制: 1000 ms 内存限制: 65536 KB 提交数: 9435 通过数: 4902 [题目描述] 题意:有一块N×M的土地, ...
- NUC1158 Lake Counting【DFS】
Lake Counting 时间限制: 1000ms 内存限制: 65536KB 通过次数: 1总提交次数: 1 问题描述 Due to recent rains, water has pooled ...
最新文章
- mysql数据库基本介绍及常见使用
- Mask-RCNN论文解读
- Android 更新项目名称
- poj3669 Meteor Shower(预处理+bfs)
- VS关闭却不关闭IIS Express并利用其进行调试
- OverflowError: Python int too large to convert to C long
- Vant(有赞)UI框架爬坑记——图片轮播+预览
- 2020中兴捧月算法大赛 埃德加考特派 区域优胜奖源代码
- 信息学奥赛一本通:1097:画矩形
- 一个有趣的模型组合预测模型
- php edm 系统,edm.php · 那些年我们一起/fanwe - Gitee.com
- 论文浏览(45) MiCT: Mixed 3D/2D Convolutional Tube for Human Action Recognition
- 联邦学习首次被纳入Gartner隐私计算技术成熟度曲线
- OSChina 周一乱弹 ——女人比代码复杂多了,搞不懂!
- uniapp写微信授权登录
- 《恋上数据结构与算法》第1季:算法概述
- JavaScript 中 mouseenter 与mouseover 区别
- 中国地理知识大全(转载)
- LockSupport的park和unpark的原理
- php设计模式:单例模式