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 ...
随机推荐
- Django创建自定义错误页面400/403/404/500等
直接参考: https://zhuanlan.zhihu.com/p/38006919 DEBUG =True的话,为开发环境,显示不了404页面.
- Windows server 2008 SSD性能测试
过渡到windows 7.windows8是趋势,老迈的windows xp .windows server 2003已经快到淘汰的阶段,安装了windows server 2008 R2 ,测试了下 ...
- [leetcode]Best Time to Buy and Sell Stock II @ Python
原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/ 题意: Say you have an array ...
- 异常捕获 UncaughtExceptionHandler MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- mybatis 乐观锁和逻辑删除
本篇介绍easymybatis如配置乐观锁和逻辑删除. 乐观锁 easymybatis提供的乐观锁使用方式跟JPA一样,使用@Version注解来实现.即:数据库增加一个int或long类型字段ver ...
- JS获取当前网页内容,创建文件并下载,URL.createObjectURL和URL.revokeObjectURL
有时候需要在前端侧对于动态生成的内容进行下载,比如页面上某一段文本信息,再比如对页面进行分享的时候,希望分享图片是页面内容的实时截图,此时,这个图片就是动态的,纯HTML显然是无法满足我们的需求的,借 ...
- mysql主从备份及原理分析
一.mysql主从备份(复制)的基本原理mysql支持单向.异步复制,复制过程中一个服务器充当主服务器,而一个或多个其它服务器充当从服务器.mysql复制基于主服务器在二进制日志中跟踪所有对数据库的更 ...
- Android -- Handling back button press Inside Fragments
干货(1) 首先创建一个抽象类BackHandledFragment,该类有一个抽象方法onBackPressed(),所有BackHandledFragment的子类在onBackPressed方法 ...
- CRF++地名实体识别(特征为词性和词)
http://x-algo.cn/index.php/2016/02/29/crf-name-entity-recognition/ 类似使用CRF实现分词和词性标注,地域识别也是需要生成相应的tag ...
- MySQL 常用函数之——substr()
substr()函数是用来截取数据库某一列字段中的一部分 常用的方式是: SBUSTR(str,pos); 就是从pos开始的位置,一直截取到最后. SUBSTR(str,pos,len); 这种表示 ...