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 ...
随机推荐
- docker logs-查看docker容器日志
只限制最后100条的日志,并持续更新日志显示 docker logs -f --tail= CONTAINER_ID docker logs -f --tail CONTAINER_ID http ...
- 不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方window.alert=function(aa){ if (typeof (aa)"undefined"){ throw "就是这";}};
不知道哪里alert undefined 用下面的语句是js报错.F12能提示报错的地方 var oldalert=window.alert; window.alert=function(aa){ i ...
- 奇怪吸引子---AnishchenkoAstakhov
奇怪吸引子是混沌学的重要组成理论,用于演化过程的终极状态,具有如下特征:终极性.稳定性.吸引性.吸引子是一个数学概念,描写运动的收敛类型.它是指这样的一个集合,当时间趋于无穷大时,在任何一个有界集上出 ...
- 一幅画<十六芒星盾>---程序员or艺术家
画上是一面含有16个尖角的铜盾,这是我用程序算法生成的图像中最震撼的一幅.图像生成出来后,我看了好久,一边看一边想我的人生转向问题:我是不是该离开苦逼又屌丝的程序界,混入高端大气上档次的艺术圈? 说要 ...
- JAVA的Split小技巧
在日常的开发中截取字符串必不可少,但是在JAVA中的Split截取有点特点的地方是 例如: String str=1,2,3,; 那么 str.split(&q ...
- 文本分类需要CNN?No!fastText完美解决你的需求(后篇)
http://blog.csdn.net/weixin_36604953/article/details/78324834 想必通过前一篇的介绍,各位小主已经对word2vec以及CBOW和Skip- ...
- 多维数组分解----SVD在推荐系统中的应用-
http://www.janscon.com/multiarray/rs_used_svd.html [声明]本文主要参考自论文<A SINGULAR VALUE DECOMPOSITION A ...
- Medline Plus
提问地址: http://apps2.nlm.nih.gov/medlineplus/contact/index.cfm?lang=en&from=http://www.nlm.nih.gov ...
- chrome中打开 swf下载的问题
https://helpx.adobe.com/cn/flash-player/kb/enabling-flash-player-chrome.html 1. 在地址栏中,键入 chrome://se ...
- Mathematica 文本界面获得之前的结果
使用%号做标记.获得文本界面之前的运算结果: