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.
Code
/*^....0^ .1 ^1^.. 011.^ 1.0^ 1 ^ ^0.11 ^ ^..^0. ^ 0^.0 1 .^.1 ^0 .........001^.1 1. .111100....01^00 ^ 11^ ^1. .1^1.^ ^0 0^.^ ^0..1.1 1..^1 .0 ^ ^00. ^^0.^^ 0 ^^110.^0 0 ^ ^^^10.01^^ 10 1 1 ^^^1110.101 10 1.1 ^^^1111110010 01 ^^ ^^^1111^1.^ ^^^10 10^ 0^ 1 ^^111^^^0.1^ 1....^11 0 ^^11^^^ 0.. ....1^ ^ ^1. 0^ ^11^^^ ^ 1 111^ ^ 0.10 00 11 ^^^^^ 1 0 1.0^ ^0 ^0 ^^^^ 0 0.0^ 1.0 .^ ^^^^ 1 1 .0^.^ ^^ 0^ ^1 ^^^^ 0. ^.11 ^ 11 1. ^^^ ^ ^ ..^^..^ ^1 ^.^ ^^^ .0 ^.00..^ ^0 01 ^^^ .. 0..^1 .. .1 ^.^ ^^^ 1 ^ ^0001^ 1. 00 0. ^^^ ^.0 ^.1. 0^. ^.^ ^.^ ^^^ ..0.01 .^^. .^ 1001 ^^ ^^^ . 1^. ^ ^. 11 0. 1 ^ ^^ 0.0 ^. 0 ^0 1 ^^^ 0.0.^ 1. 0^ 0 .1 ^^^ ...1 1. 00 . .1 ^^^ ..1 1. ^. 0 .^ ^^ ..0. 1. .^ . 0 ..1 1. 01 . . ^ 0^.^ 00 ^0 1. ^ 1 1.0 00 . ^^^^^^ ..^ 00 01 ..1. 00 10 1 ^^.1 00 ^. ^^^ .1.. 00 .1 1..01 ..1.1 00 1. ..^ 10^ 1^ 00 ^.1 0 1 1.1 00 00 ^ 1 ^. 00 ^.^ 10^ ^^1.1 00 00 10^..^ 1. ^. 1.0 1 ^. 00 00 .^^ ^. ^ 1 00 ^0000^ ^ 011 0 ^. 00.0^ ^00000 1.00.1 11. 1 0 1^^0.01 ^^^ 01.^ ^ 1 1^^ ^.^1 1 0... 1 ^1 1^ ^ .01 ^ 1.. 1.1 ^0.0^ 0 1..01^^100000..0^1 1 ^ 1 ^^1111^ ^^0 ^ ^ 1 1000^.1 ^.^ . 00.. 1.1 0. 01. . 1. .^1. 1 1. ^0^ . ^.1 00 01^.0 001. .^*/
// Virtual_Judge —— Lake Counting POJ - 2386.cpp created by VB_KoKing on 2019-05-05:15.
/* Procedural objectives:Variables required by the program:Procedural thinking:
从任意的W开始, 不停地把邻接的部分用.代替。1次DFS后与初始的这个W连接的所有W就都被替换成了.,因此直到图中不在存在W位置,总共进行DFS的次数就是答案。8个方向对应了8种状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8*N*M)。Functions required by the program:*/
/* My dear Max said:
"I like you,
So the first bunch of sunshine I saw in the morning is you,
The first gentle breeze that passed through my ear is you,
The first star I see is also you.
The world I see is all your shadow."FIGHTING FOR OUR FUTURE!!!
*/
#include <iostream>
using namespace std;int N,M;
char field[107][107];void dfs(int x,int y)
{field[x][y]='.';for (int dx = -1; dx < 2; dx++){for (int dy = -1; dy < 2; dy++){int nx=x+dx,ny=y+dy;if (-1<nx&&nx<N+1&&-1<ny&&ny<M&&field[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 (field[i][j]=='W'){dfs(i,j);res++;}}}cout<<res<<endl;
}int main()
{cin>>N>>M;for (int i = 0; i < N; i++)for (int j = 0; j < M; j++)cin>>field[i][j];solve();return 0;
}
Lake Counting POJ - 2386相关推荐
- DFS:深入优先搜索 POJ-2386 Lake Counting
深度优先搜索是从最开始的状态出发,遍历所有可以到达的状态. 因此可以对所有的状态进行操作,或列举出所有的状态. Lake Counting POJ - 2386 Due to recent rains ...
- POJ 2386 Lake Counting
链接:http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submis ...
- 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(DFS)
深度优先搜索属于图算法的一种,英文缩写为DFS即Depth First Search.其过程简要来说是对每一个可能的分支路径深入到不能再深入为止,而且每个节点只能访问一次. 举例说明之:下图是一个无向 ...
- 信息学奥赛一本通(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 ...
- bzoj 3385: [Usaco2004 Nov]Lake Counting 数池塘(DFS)
3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 78 Solved: 6 ...
- c++ Lake Counting
Lake Counting(bfs dfs) 题目描述 样例输入 10 12 W . . . . . . . . W W . . W W W . . . . . W W W . . . . W W . ...
最新文章
- Silverlight实例教程 - Out of Browser的自定义应用
- 10.1引用数据类型的转换
- TF之LSTM:利用多层LSTM算法对MNIST手写数字识别数据集进行多分类
- 通俗易懂讲解自适应提升算法AdaBoost
- 《Office 365开发入门指南》上市说明和读者服务
- C程序员要学C++吗?
- 智慧城市产业图谱研究报告(2020年)
- 开局说丑说拒绝,开售抢的贼快!iPhone11预约超百万
- [Active Learning] Multi-Criteria-based Active Learning
- python是开源的.它可以被移植_python是开源的,它可以被移植到许多平台上,是对的吗?...
- 雷达发现 |最新教育行业数据报告
- 利用ev3dev编程
- 他是中国最牛X的黑客,曾让6个国家束手无策,却被怀疑是精神病
- 汇编语言-字符串大写转小写,小写转大写
- 板绘技巧:水晶怎么画?如何画出晶莹剔透的效果?
- 【漏洞复现-EmpireCms-文件上传】vulfocus/empirecms-cve_2018_18086
- 点云配准算法ICP及其各种变体
- 对 oc 学习的 阶段反思
- 第四章 C语言输入输出_C语言数据输出大汇总以及轻量进阶
- android7 强制gpu渲染,安卓手机开启强制GPU渲染功能的方法
热门文章
- 抽象方法《Thinking in Java》随笔014
- Javascript aop(面向切面编程)之around(环绕)
- StarUML启动报RPC服务器不可用错误
- linux经常使用解压缩命令
- SDUT ACM 2144 最小生成树,克鲁斯卡尔模板
- struts1.x心得1--struts入门介绍
- POJ 3349 Snowflake Snow Snowflakes
- java: 十六进制转八进制
- typescript 怎么表示当前时间减一个月_TypeScript 入门知识点总结
- iOS url出现特殊字符处理 -- stringByAddingPercentEncodingWithAllowedCharacters