1388:Lake Counting
题目链接:
NOI题库http://noi.openjudge.cn/ch0205/1388/
POJ 2386 http://poj.org/problem?id=2386
- 总时间限制: 1000ms 内存限制: 65536kB
- 描述
- 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.
- 输入
- * 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.
- 输出
- * Line 1: The number of ponds in Farmer John's field.
- 样例输入
-
10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W. - 样例输出
-
3
- 提示
- OUTPUT DETAILS:
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
题目大意:
有一块N*M的土地,雨后机起了水,有水标记为'W',干燥标记为'.'。
八连通的积水被认为是连接在一起的。需要求出院子里有多少水洼。
首先输入N和M,然后输入N*M的二维字符数组,行内字符之间没有空格。
输出水洼的数目。
N和M都在100以内。
算法分析:
这道题可以用深搜,也可以用广搜。
扫描二维数组,每遇到一个'W'就从这个地方出发开始深搜或广搜。
搜索过程中,把搜缩遇到的'W'全部设为'.'。
每当完成一次深搜或广搜,水洼数增1.
题解可以参考:
http://blog.csdn.net/c20180630/article/details/52329915
http://www.cnblogs.com/sooner/archive/2013/04/09/3010938.html
深搜的思路:

下面是我自己写的代码,含深搜与广搜的代码:
#include<stdio.h>
#include<iostream>
#include<queue>
#include<stack>
using namespace std; struct obj
{
int xx,yy;
}; int N,M;
char a[][];
int Count; int dx[]={-,-,,,,,,-};//从正上方开始,顺时针旋转的8个方向
int dy[]={,,,,,-,-,-};
void BFS(int x,int y);//从(x,y)开始广搜
void DFS(int x,int y);//从(x,y)开始深搜
void DFS2(int x,int y);//从(x,y)开始深搜,递归实现 int main(int argc, char *argv[])
{
freopen("1388.in","r",stdin);
int i,j;
scanf("%d%d",&N,&M);getchar();//吸收回车符
for(i=;i<N;i++)
{
gets(a[i]);
/*
for(j=0;j<M;j++)
{
a[i][j]=getchar();
}
getchar();//吸收回车符
*/
} Count=;
for(i=;i<N;i++)
{
for(j=;j<M;j++)
{
if(a[i][j]=='W')
{
BFS(i,j);//从(i,j)开始广搜
//DFS(i,j);//从(i,j)开始深搜
//{ DFS2(i,j); Count=Count+1;} //递归实现的深搜,从(i,j)开始深搜
}
}
}
printf("%d\n",Count);
return ;
}
void BFS(int x,int y)//从(x,y)开始广搜
{
queue<struct obj> q;
struct obj start,temp;
int i,txx,tyy; start.xx=x;
start.yy=y;
q.push(start);
a[x][y]='.';
while(!q.empty())
{
for(i=;i<;i++)
{
txx=q.front().xx+dx[i];
tyy=q.front().yy+dy[i];
if(txx>=&&txx<N&&tyy>=&&tyy<M&&a[txx][tyy]=='W')
{
temp.xx=txx;
temp.yy=tyy;
a[txx][tyy]='.';
q.push(temp);
}
}
q.pop();
}
Count++;
} void DFS(int x,int y)//从(x,y)开始深搜
{
stack<struct obj> s;
struct obj start,temp,temp2;
int i,txx,tyy; a[x][y]='.';
start.xx=x;
start.yy=y;
s.push(start);
while(!s.empty())
{
temp=s.top(); s.pop();
for(i=;i<;i++)
{
txx=temp.xx+dx[i];
tyy=temp.yy+dy[i];
if(txx>=&&txx<N&&tyy>=&&tyy<M&&a[txx][tyy]=='W')
{
temp2.xx=txx;
temp2.yy=tyy;
a[txx][tyy]='.';
s.push(temp2);
}
}
}
Count++;
} void DFS2(int x,int y)//从(x,y)开始深搜,递归实现
{
int i,txx,tyy;
a[x][y]='.';
for(i=;i<;i++)
{
txx=x+dx[i];
tyy=y+dy[i];
if(txx>=&&txx<N&&tyy>=&&tyy<M&&a[txx][tyy]=='W')
{
//a[txx][tyy]='.';
DFS2(txx,tyy);
}
}
}
1388:Lake Counting的更多相关文章
- Openjudge1388 Lake Counting【DFS/Flood Fill】
http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制: 1000ms 内存限制: ...
- 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 ...
随机推荐
- [转]五种常见的 PHP 设计模式
FROM : http://www.ibm.com/developerworks/cn/opensource/os-php-designptrns/ 设计模式 一书将设计模式引入软件社区,该书的作者是 ...
- JavaScript:Number 对象
ylbtech-JavaScript:Number 对象 1. Number 对象返回顶部 Number 对象 Number 对象是原始数值的包装对象. 创建 Number 对象的语法: var my ...
- Mockito 的使用
转自:Mockito 中文文档 ( 2.0.26 beta ) 转自:手把手教你 Mockito 的使用 参数匹配器 Argument Matcher(参数匹配器) Mockito通过equals() ...
- 秒懂,Java 注解 (Annotation)你可以这样学
转自: https://blog.csdn.net/briblue/article/details/73824058 文章开头先引入一处图片. 这处图片引自老罗的博客.为了避免不必要的麻烦,首先声明我 ...
- 使用SetUnhandledExceptionFilter转储程序崩溃时内存DMP .
关于程序崩溃时转储内存DMP,可以设置注册表,使程序崩溃时自动转储内存DMP,见程序崩溃时利用注册表自动转储内存DMP.本文要介绍的是使用SetUnhandledExceptionFilter函数在程 ...
- Focal Loss(RetinaNet) 与 OHEM
Focal Loss for Dense Object Detection-RetinaNet YOLO和SSD可以算one-stage算法里的佼佼者,加上R-CNN系列算法,这几种算法可以说是目标检 ...
- 求职之C++小知识点整理
1.顺序容器 1.顺序容器:vector,deque,list,forward_list,array,string.其中除list和forward_list外,其它都支持快速随机访问. deque a ...
- MarkDownPad Pro 支持github格式的markdown语法
1. http://blog.csdn.net/xiaohei5188/article/details/43964451
- DaoCloud加速docker镜像下载
1. 注册DaoCloud用户; 2. 注册完成后,会进入dashboard页面,点击右上方的加速器.该页面提供了Linux.Windows和Mac的加速方案,我这里选择的是Linux: 3. 执行其 ...
- 杂谈:大容量(T级容量)的网盘的意义
这两天,大容量的网盘的消息不断的推出.有百度的网盘推1T容量的:有腾讯的推10T容量的:有的还推不限容量的等等不一而足. 先看看大容量网盘的历史 早先是没有网盘这个概念的.能提供免费空间是电子邮箱 早 ...