Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 23950   Accepted: 12099

Description

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.

Input

* 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.

Output

* Line 1: The number of ponds in Farmer John's field.

Sample Input

10 12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.

Sample Output

3

这个题可以用深搜也可以用广搜,我就是从这个题,明白了两种搜索方式的不同

大家来体会一下吧!

DFS版:
 #include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; char map[][];
int mov[][]={,,,-,-,,,,,,-,-,,-,-,};
int m,n;
bool can(int x ,int y)//判断这个点能不能走
{
if(x<||x>m-||y<||y>n-||map[x][y]=='.')
return false;
return true;
} void dfs(int x,int y)
{
int i,xx,yy;
for(i=;i<;i++)
{
xx=x+mov[i][];
yy=y+mov[i][];
if(can(xx,yy))
{
map[xx][yy]='.';//走一个点就标记一下
dfs(xx,yy);
}
}
}
int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
int sum=;
for(i=;i<m;i++)
scanf("%s",map[i]);
for(i=;i<m;i++)
{
for(j=;j<n;j++)
{
if(map[i][j]=='W')
{
map[i][j]='.';
dfs(i,j);//每次进入搜索函数就把这个点周围能走的点走完
sum++;
}
}
}
printf("%d\n",sum);
}
return ;
}
BFS版:
 #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
char map[][];
int m,n;
int mov[][]={,,,-,,,-,,,,-,-,,-,-,};
struct node
{
int a,b;
}ta,tb;//定义一个结构体用来存坐标
bool can(node x)
{
if(x.a<||x.a>m-||x.b<||x.b>n-||map[x.a][x.b]=='.')
return false;
return true;
} void bfs(int x,int y)
{
queue<node> q;
ta.a=x;
ta.b=y;
q.push(ta);//入队
while(!q.empty())//直到把队列能访问的都访问过
{
int i;
ta=q.front();
q.pop();
for(i=;i<;i++)
{
tb.a=ta.a+mov[i][];
tb.b=ta.b+mov[i][];
if(can(tb))
{
map[tb.a][tb.b]='.';
q.push(tb);//如果可以访问就入队
}
}
}
} int main()
{
int i,j;
while(scanf("%d%d",&m,&n)!=EOF)
{
int sum=;
for(i=;i<m;i++)
scanf("%s",map[i]);
for(i=;i<m;i++)
for(j=;j<n;j++)
{
if(map[i][j]=='W')
{
map[i][j]='.';
bfs(i,j);
sum++;
}
}
printf("%d\n",sum);
}
return ;
}
												

Lake Counting--poj2386的更多相关文章

  1. Poj2386 Lake Counting (DFS)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 49414   Accepted: 24273 D ...

  2. POJ2386 Lake Counting 【DFS】

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20782   Accepted: 10473 D ...

  3. 【POJ - 2386】Lake Counting (dfs+染色)

    -->Lake Counting 直接上中文了 Descriptions: 由于近日阴雨连天,约翰的农场中中积水汇聚成一个个不同的池塘,农场可以用 N x M (1 <= N <= ...

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

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

  5. POJ 2386 Lake Counting(深搜)

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

  6. POJ 2386 Lake Counting

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

  7. bzoj1751 [Usaco2005 qua]Lake Counting

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

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

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

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

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

  10. 1751: [Usaco2005 qua]Lake Counting

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

随机推荐

  1. 宽带连接工具[bat]

    功能概述: 本工具使用批处理编写,提供自动判断网络状态以决定断开或是连上网络,本月已用宽带时长,到月初自动清零.提供联网日志功能,可以记录下所有的连接或断开网络记录.如果连接失败,自动提示输入密码,特 ...

  2. Maven的使用--Eclipse在线安装Maven插件m2e

    我使用的Eclipse版本是3.7(Indigo) 通过Eclipse的help选项,点击“Install New Software...”弹出安装对话框, 点击add按钮,在Location里输入h ...

  3. css3的loadding效果

    <!DOCTYPE html> <html> <head> <title>CSS3 loading效果</title> <meta c ...

  4. Android DatePickerDialog 只选择年月

    //对EditText注册OnTouch事件etSscxNssbDate.setOnTouchListener(selectDateTouchListener); //选择日期 private OnT ...

  5. Qt之窗口动画(下坠、抖动、透明度)(还有好多相关帖子)

    简述 前面几节中我们介绍了关于动画的基本使用,有属性动画.串行动画组.并行动画组.这节我们来实现一些特效,让交互更顺畅. 简述 示例 效果 源码 更多参考 示例 下面,我们以geometry.pos. ...

  6. QT---线程间通信(要先编译libqt-mt.so?)

    在 Qt 系统中,运行着一个GUI 主事件线程,这个主线程从窗口系统中获取事件,并将它们分发到各个组件去处理.在 QThread 类中有一种从非主事件线程中将事件提交给一个对象的方法,也就是 QThr ...

  7. C# 如何查看源程序的IL代码

    1.打开microsoft  visual  studio  2008  /  visual  studio  tools /  visual  studio  2008 命令提示  ,并输入ilda ...

  8. DM365视频处理流程/DM368 NAND Flash启动揭秘

    出自http://blog.csdn.net/maopig/article/details/7029930 DM365的视频处理涉及到三个相关处理器,分别是视频采集芯片.ARM处理器和视频图像协处理器 ...

  9. css案例学习之div ul li a 实现导航效果

    效果 代码 <html> <head> <title>无需表格的菜单</title> <style> body{ background-co ...

  10. cf475B Strongly Connected City

    B. Strongly Connected City time limit per test 2 seconds memory limit per test 256 megabytes input s ...