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 ...
随机推荐
- [转]InnoDB和MyISAM区别
From : http://blog.csdn.net/ghosc/article/details/5391544 MySQL作为当前最为流行的免费数据库服务引擎,已经风靡了很长一段时间,不过也许也有 ...
- python的rsa公钥解密方法
示例: # -*- coding: UTF- -*- import M2Crypto import base64 #私钥加密,公钥解密 def pri_encrypt(msg, file_name): ...
- JQuery的ajaxFileUpload的使用
https://www.cnblogs.com/zhanghaoliang/p/6513964.html 最近在工作中使用了Jquery的ajaxFileUpload的图片上传插件,感觉这种异步上传的 ...
- ASP.NET MVC:WebPageRenderingBase.cs
ylbtech-funcation-Utility: ASP.NET MVC:WebPageRenderingBase.cs 提供用于呈现使用 Razor 视图引擎的页的方法和属性. 1.A,WebP ...
- OpenWRT - WEB界面开发思路和基本方法
想要对OpenWRT的WEB界面(*下称界面)进行修改.修改的目标是: 1.修改页面的样式,设计为企业的风格(stylesheet) 2.新建自己的功能,实现 访问页面后,用户就可以对配置文件(也就是 ...
- go语言之进阶篇通过range遍历channel内容
1.通过range遍历channel内容 package main import ( "fmt" ) func main() { ch := make(chan int) //创建 ...
- 解决easyui combobox不能默认选中
开始出现很奇怪的问题,ff没有问题IE8还是会出现不能选中默认项的问题,更改了select.input的方式,数据加载方式也更改为json的方式,未果,最后将datagrid toolbar的初始化放 ...
- windows取证
工具网站 : http://www.ntsecurity.nu/toolbox/ 命令行历史 :命令行模式 CMD 中使 doskey /history 命令可以显示前面输入的命令情况(例如使用 cl ...
- 在Cygwin里,如何进入到C盘?
答: cd /cygdrive/c 来源: How to navigate to a directory in C:\ with Cygwin? https://stackoverflow.com/q ...
- Android -- java代码设置margin
我们平常可以直接在xml里设置margin,如: <ImageView android:layout_margin="5dip" android:src="@dra ...