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. javascript事件捕获与冒泡

    对“捕获”和“冒泡”这两个概念,我想我们对冒泡更熟悉一些,因为在我们使用的所有浏览器中,都支持事件冒泡,即事件由子元素向祖先元素传播的,就 像气泡从水底向水面上浮一样.而在像firefox,chrom ...

  2. Mysql MERGE 引擎在分表环境下得使用

    应用场景:当我们在做分表的时候,通常会把一个大表的数据拆分成若干个分表,这也是数据库优化中的分表概念.随着分表随之而来的问题就是多表查询. 现在有 t1,t2 两张数据表,需要满足不同的查询条件同时从 ...

  3. C51编译器的扩展关键字

    附表1-1 ANSIC标准关键字 附表1-2 C51编译器的扩展关键字

  4. WPF笔记(2.3 StackPanel)——Layout

    原文:WPF笔记(2.3 StackPanel)--Layout StackPanel用于小规模的排版布局,比如说一个局部下几个textbox和Button啦.Orientation属性有Vertic ...

  5. CreateThread简单那多线程编程

    CreateThread简单那多线程编程 作者:vpoet mail:vpoet_sir@163.com 在进行多任务处理的时候我们往往会用到多线程技术,多线程理论上是多个线程同事处理不同的工作,但是 ...

  6. 运用 BoxLayout 进行 Swing 控件布局

    摘自http://www.cnblogs.com/fnlingnzb-learner/p/6008572.html 运用 BoxLayout 进行 Swing 控件布局 对于初学 Java Swing ...

  7. tr转换或删除字符

    字符处理命令:tr —— 转换或删除字符 逐个字符处理而不是处理单词的tr [OPTION]... SET1 [SET2]    -d: 删除出现在字符集中的所有字符 tr ab AB

  8. Oracle学习笔记(1)——查询及删除重复数据

      1.查找表中多余的重复记录(根据单个字段studentid)   select * from table_name where studentid in (select studentid fro ...

  9. 检查ORACLE的警告文件的脚本

    检查两天内的须要重视的信息: vi   alter_error.sh echo "Check Alter Error:" cat $TRACE/alert_$ORACLE_SID. ...

  10. C程序设计语言(K&R)笔记

    1.表达式中float类型的操作数不会自动转换为double类型.一般来说,数学函数(如math.h)使用双精度类型的变量.使用float类型主要是为了在使用较大数组时节省存储空间,有时也为了节省机器 ...