D - Lake Counting
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.
搜索水题,只要往四周一直搜,搜过的改变值就可以,能搜几次就是几个;
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include <iomanip>
#include<cmath>
#include<float.h>
#include<string.h>
#include<algorithm>
#define sf scanf
#define pf printf
#define pb push_back
#define mm(x,b) memset((x),(b),sizeof(x))
#include<vector>
#include<map>
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=a;i>=n;i--)
typedef long long ll;
typedef long double ld;
typedef double db;
const ll mod=1e12+100;
const db e=exp(1);
using namespace std;
const double pi=acos(-1.0);
char a[105][105];
bool dfs(int x,int y)
{
// cout<<x<<" "<<y<<endl;
if(a[x][y]!='W') return false;
a[x][y]='a';//把经过的位置改变
if(a[x-1][y-1]=='W')//左上
dfs(x-1,y-1);
if(a[x-1][y+1]=='W')//右上
dfs(x-1,y+1);
if(a[x+1][y-1]=='W')//左下
dfs(x+1,y-1);
if(a[x+1][y+1]=='W')//右下
dfs(x+1,y+1);
if(a[x-1][y]=='W')//上
dfs(x-1,y);
if(a[x][y-1]=='W')//左
dfs(x,y-1);
if(a[x][y+1]=='W')//右
dfs(x,y+1);
if(a[x+1][y]=='W')//下
dfs(x+1,y);
return true;
}
int solve(int n,int m)
{
int sum=0;
rep(i,1,n+1)
{
rep(j,1,m+1)
if(dfs(i,j))
sum++;
}
return sum;
}
int main()
{
int n,m;
sf("%d%d%d%d",&n,&m);
mm(a,'.');
rep(i,1,n+1)
{
sf("%s",&a[i][1]);
//pf("1%s\n",&d[i]);
a[i][m+1]='.';
}
pf("%d\n",solve(n,m));
}
D - Lake Counting的更多相关文章
- 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(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- bzoj1751 [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MB Submit: 168 Solved: 130 [ ...
- BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘
题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MB Description 农夫 ...
- 3385: [Usaco2004 Nov]Lake Counting 数池塘
3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 22 Solved: 21 ...
- 1751: [Usaco2005 qua]Lake Counting
1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 190 Solved: 150[Su ...
- 洛谷 P1596 [USACO10OCT]湖计数Lake Counting
题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
随机推荐
- 加密入门(三):TrueCrypt(转)
http://terrychen.info/encryption-truecrypt/ TrueCrypt 是一款功能强大的开源加密工具,利用 TrueCrypt 可以创建一个加密文件作为虚拟加密卷, ...
- SQLSERVER 设置默认值
DECLARE @test intSET @test=nullselect isnull(@test,0)
- 重写$.ajax方法
/*重写Jquery中的ajax 封装壳*/ $(function () { (function ($) { //首先备份下jquery的ajax方法 var _ajax = $.ajax; //重写 ...
- JavaScript深入系列15篇
JavaScirpt深入之从原型到原型链 构造函数创建对象 我们先使用构造函数创建一个对象: function Person() { } var person = new Person(); pers ...
- 学习一个Vue模板项目
最开始学习Vue的时候,不建议直接使用模板,而应该自己从头写起.模板都是人写的,要坚信"人能我能".只有自己亲自实践,才能促进自己主动思考,才能对模板.框架有深刻的理解. 在Git ...
- Rplidar学习(五)—— rplidar使用cartographer_ros进行地图云生成
一.Cartographer简介 Cartographer是google开源的通用2D和3D定位与地图同步构建的SLAM工具,并提供ROS接口.官网地址:https://github.com/goog ...
- android用TextView实现跑马灯效果
今天搞啦很久,其实很简单,就加几个属性就可以啦! 图如下 : 有的说要重写TextView方法,有的说要设置固定长度,但是我没重写也没有设置固定长度也弄出来啦!跑在2.3.3的手机上面.就是不知道其他 ...
- Linux install svn server
ref: http://blog.csdn.net/pingnanlee/article/details/8812520 1. yum -y install subversion 2. svnadmi ...
- 利用linux的mtrace命令定位内存泄露(Memory Leak)
一谈到内存泄露, 多数程序猿都闻之色变. 没错, 内存泄露非常easy引入. 但非常难定位. 以你我的手机为例(如果不常常关机). 如果每天泄露一些内存, 那么開始的一个星期, 你会发现手机好好的. ...
- 通过MTK迁移Mysql到EDB实战指南
1.1 迁移准备 下图是Migration toolkit(MTK)可使用的迁移功能 1 查看一下迁移源数据库testdb信息.共三张表 watermark/2/text/aHR0cDovL2Jsb2 ...