题目链接:

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的更多相关文章

  1. Openjudge1388 Lake Counting【DFS/Flood Fill】

    http://blog.csdn.net/c20182030/article/details/52327948 1388:Lake Counting 总时间限制:   1000ms   内存限制:  ...

  2. POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)

    来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS   Memory Limit: 65536 ...

  3. POJ 2386 Lake Counting(深搜)

    Lake Counting Time Limit: 1000MS     Memory Limit: 65536K Total Submissions: 17917     Accepted: 906 ...

  4. POJ 2386 Lake Counting

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 28966   Accepted: 14505 D ...

  5. bzoj1751 [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 168  Solved: 130 [ ...

  6. BZOJ 3385: [Usaco2004 Nov]Lake Counting 数池塘

    题目 3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MB Description     农夫 ...

  7. 3385: [Usaco2004 Nov]Lake Counting 数池塘

    3385: [Usaco2004 Nov]Lake Counting 数池塘 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 22  Solved: 21 ...

  8. 1751: [Usaco2005 qua]Lake Counting

    1751: [Usaco2005 qua]Lake Counting Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 190  Solved: 150[Su ...

  9. 洛谷 P1596 [USACO10OCT]湖计数Lake Counting

    题目链接 https://www.luogu.org/problemnew/show/P1596 题目描述 Due to recent rains, water has pooled in vario ...

随机推荐

  1. Just-In-Time Debugging in Visual Studio 禁止VS在服务器上调试

    To disable Just-In-Time debugging by editing the registry On the Start menu, search for and run rege ...

  2. HttpContext.Current.Session 和 Session 的区别

    Session(会话)通常指一个动作从开始到结束不间断的一个动作. 例如“打电话”,通常是“1.拿起电话--2.拨对方号码--3.对方截图--4.挂机”.这四个步骤从完成到结束组成了一个基本的Sess ...

  3. C#7.0新增功能点

    原文地址:  https://www.cnblogs.com/runningsmallguo/p/8972678.html 第二部分:C#7.0新增的功能 (1)数字字面量的提升: C#7中的数字文字 ...

  4. @Autowired用法详解

    @Autowired 注释,它可以对类成员变量.方法及构造函数进行标注,完成自动装配的工作. 通过 @Autowired的使用来消除 set ,get方法.在使用@Autowired之前,我们对一个b ...

  5. go语言之进阶篇无缓冲channel

    1.无缓冲channel 示例: package main import ( "fmt" "time" ) func main() { //创建一个无缓存的ch ...

  6. iOS图标抖动效果

    开始抖动 -(void)BeginWobble { srand([[NSDate date] timeIntervalSince1970]); float rand=(float)random(); ...

  7. Focal Loss(RetinaNet) 与 OHEM

    Focal Loss for Dense Object Detection-RetinaNet YOLO和SSD可以算one-stage算法里的佼佼者,加上R-CNN系列算法,这几种算法可以说是目标检 ...

  8. 全景分割panopticapi使用

    文件解析 参考github:https://github.com/cocodataset/panopticapi 输入图像:

  9. Ajax核心技术之XMLHttpRequest

    Ajax:即"Asynchronous JavaScript and XML"(异步JavaScript和XML),一门综合性的技术:运用JavaScript对象XMLHttpRe ...

  10. 十个 Laravel 5 程序优化技巧

    性能一直是 Laravel 框架为人诟病的一个点,所以调优 Laravel 程序算是一个必学的技能. 接下来分享一些开发的最佳实践,还有调优技巧,大家有别的建议也欢迎留言讨论. 这里是简单的列表: 配 ...