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 ...
随机推荐
- vulhub-php/php_inclusion_getshell
注:本地测试php文件包含+phpinfo泄露导致getshell,此漏洞与php版本无关 使用vulhub环境进行复现: 项目地址:https://github.com/vulhub/vulhub/ ...
- U盘安装Ubuntu15.04 出现boot failed: please change disks and press a key to continue
1.根据国内的教程,用Ultraiso制作了一个Ubuntu15.04的U盘启动盘,在装系统的时候提示如下错误:boot failed: please change disks and press a ...
- <ganglia+nagios>rhel6.5
由于linux下的office和win下有所区别,我只能把linux下的.dot文件打包成pdf,粘贴发送标出来,但有些图片还是没办法发表,要是有朋友感兴趣的话,可加我qq 215687833具体的文 ...
- Sublime Text3安装SublimeGit插件
之前一直用PhpStorm作为开发工具,但是最近使用的一台电脑上安装的老是卡死,只好用回Sublime Text3. 搜索后打开第一个链接“Sublime Text 3中使用SublimeGit插件” ...
- 合理设置apache的最大连接数
手头有一个网站在线人数增多,访问时很慢.初步认为是服务器资源不足了,但经反复测试,一旦连接上,不断点击同一个页面上不同的链接,都能迅速打开,这种现象就是说明apache最大连接数已经满了,新的访客只能 ...
- JS实现正则表达式
一.创建正则表达式 一共有两种方式: 1.直接量:var re = /[0-9]*/; 2.通过RegExp对象的构造函数:var re = RegExp("[0-9]*",&qu ...
- 部署JavaWeb时出现 If a file is locked,you can wait until
在部署JavaWeb程序时出现了if a file is locked ,you can wait until the lock stop的问题,这个主要是classpath目录出错或者jar包未导入 ...
- pat乙级1052
输出“\”字符: cout << "\\"; 因为‘\’是转义字符,例如“\n”代表换行. 同理,printf输出“%”: printf("%%") ...
- 300行ABAP代码实现一个最简单的区块链原型
不知从什么时候起,区块链在网上一下子就火了. 这里Jerry就不班门弄斧了,网上有太多的区块链介绍文章.我的这篇文章没有任何高大上的术语,就是300行ABAP代码,实现一个最简单的区块链原型. 我个人 ...
- 用户在设置密码时,提醒请输入半角字符(vue+element+valid)
要保证callback()只有一个出口 rules:{ newPassword: [{validator:(rule,newPassword,callback)=>{ var all = fal ...