Lake Counting(dfs)
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.
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
char a[100][100];
int m=0,n=0;
int ad(int x,int y)
{
a[x][y]='.';
for(int i=-1; i<2; i++)
for(int j=-1; j<2; j++)
if(a[x+i][y+j]=='W'&&x+i>=0&&x+i<n&&y+j>=0&&y+j<m)
ad(x+i,y+j);
}
int main()
{
int ans=0;
scanf("%d%d",&n,&m);
getchar();
for(int i=0; i<n; i++)
{
for(int j=0; j<m; j++)
scanf("%c",&a[i][j]);
getchar();
}
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
if(a[i][j]=='W')
{
ad(i,j);
ans++;
}
printf("%d\n",ans);
return 0;
}
Sample Output
3
Lake Counting(dfs)的更多相关文章
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
- [USACO10OCT]Lake Counting(DFS)
很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 【POJ - 2386】Lake Counting (dfs+染色)
-->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- LeetCode Subsets (DFS)
题意: 给一个集合,有n个互不相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: DFS方法:由于集合中的元素是不可能出现相同的,所以不用解决相同的元素而导致重复统计. class Sol ...
随机推荐
- springboot 学习笔记(六)
(六)springboot整合activemq 1.现下载activemq,下载链接:http://activemq.apache.org/download.html,windows系统解压后进入bi ...
- jstack的使用方法
背景 记得前段时间,同事说他们测试环境的服务器cpu使用率一直处于100%,本地又没有什么接口调用,为什么会这样?cpu使用率居高不下,自然是有某些线程一直占用着cpu资源,那又如何查看占用cpu较高 ...
- 利用python进行简单的图片处理
python的 PIL模块是专门用来处理图片的模块,其功能可以说是非常强大了. 演示环境:win7 操作系统 安装python2.7以及指定的对应PIL模块 我这里有一个现成的PIL模块.本文的大部分 ...
- grunt + sass 使用记录
环境依赖 Nodejs for grunt Ruby for sass 配置文件 package.json { "name": "app", "ver ...
- DB2数据库常用语句
1.快速清空大量数据表数据,但是还原不了 alter table rm_customer activate not logged initially with empty table2.大量导出表语句 ...
- Java反序列化漏洞详解
Java反序列化漏洞从爆出到现在快2个月了,已有白帽子实现了jenkins,weblogic,jboss等的代码执行利用工具.本文对于Java反序列化的漏洞简述后,并对于Java反序列化的Poc进 ...
- ajax请求成功后js刷新当前页,当前页是post查询结果(用post请求进行搜索筛选)的问题
下面的一个ajax操作,原先操作成功会刷新当前页,保证用户看到的数据是最新的,一般情况不会出现问题.$.ajax({ url: url + "/addTeacherAuth", / ...
- MovieReview—Avengers: Infinity War(复仇者联盟3:无限战争)
Antagonist? Thanos,the central figure of the Avengers 3,antagonist. Everyone has his own ideals and ...
- Head First HTML与CSS阅读笔记(一)
之前写过不少前端界面,但是没有完整阅读过一本HTML与CSS的书籍,都是用到什么查什么,最近闲暇之余想巩固加深一下前端基础方面的知识,阅读了<Head First HTML与CSS>,感觉 ...
- IOS 隐式动画(非Root Layer)
● 每一个UIView内部都默认关联着一个CALayer,我们可用称这个Layer为Root Layer(根 层) ● 所有的非Root Layer,也就是手动创建的CALayer对象,都存在着隐式动 ...