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. 轻量jquery框架之--组件交互基础设计

    概要 组件交互基础,即考虑在JQUERY对象下($)下扩展所有组件都需要用到的通用api,如ajax入口.对表单的操作.html片段加载.通用的配合datagrid通用的curd客户端对象等. 扩展a ...

  2. java普通类如何得到spring中的bean类

    在SSH集成的前提下.某些情况我们需要在Action以外的类中来获得Spring所管理的Service对象. 之前我在网上找了好几好久都没有找到合适的方法.例如: ApplicationContext ...

  3. Sumblime Text 2 常用插件以及安装方法

    1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Packages目录(菜单->preferences->packages). 2.使用Package C ...

  4. ThinkPhp调用webservice

    模板页: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <titl ...

  5. HDU 题目分类收集

    并查集题型简单并查集1213 How Many Tables 1232 畅通工程 (杭电简单的并查集不是很多) 简单最小生成树1233 还是畅通工程 1863 畅通工程 1874 畅通工程再续 187 ...

  6. c# 大数据量比较时-方案

    1.当面临千万条数据量的比较时,从技术的角度来说应该用泛型键值(c#键值由于用了散列算法速度很快).例如前几天我需要查的是 航空公司.出发.到达.返点可以将 航空公司-出发-到达做一个键,返点作为值. ...

  7. WebSocket C# Demo

    WebSocket 规范 WebSocket 协议本质上是一个基于 TCP 的协议.为了建立一个 WebSocket 连接,客户端浏览器首先要向服务器发起一个 HTTP 请求,这个请求和通常的 HTT ...

  8. C语言简单strcat和strcmp的实现

    对于C标准库中的字符串处理函数应该平常用的比较多:简单实现strcat和strcmp _strcpy: char *_strcpy(char *dest, char *src) { char *buf ...

  9. [Medusa-dev] psp_handler - embed python in HTML like ASP

    [Medusa-dev] psp_handler - embed python in HTML like ASP [Medusa-dev] psp_handler - embed python in ...

  10. ActionForward

    一.只有登录才能显示的页面 这是一个很平常的问题,在访问某些网页的时候,只有登录才可以访问,以此保证安全. 实现原理也很简单,就是将一个属性设置在session中.在访问的时候进行判断即可. 例:re ...