Lake Counting
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 25322   Accepted: 12759

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

思路:
很基础的一道题目,入门级别,有助于理解搜索的本质以及bfs和dfs的区别
这个题可以用“扫雷”的思维来形象的理解,即遇到一个'W',点一下这个点,则与它相邻的一片为‘W’的点都变成‘.’了
而我们只需要从头开始遍历一共有多少个这样的W即可

dfs:
#include <stdio.h>
#define maxn 107 int n,m;
char g[maxn][maxn];
int dir[][] = {{-,-},{-,},{-,},{,-},{,},{,},{,-},{,}}; void dfs(int x,int y){
g[x][y] = '.';//此行代码意义重大,相当于将其置为已访问状态
for(int i = ;i < ;i++) {
int dx = x+dir[i][];
int dy = y+dir[i][];
if(dx>n||dx<||dy<||dy>m)
continue;
if(g[dx][dy] == '.')
continue;
dfs(dx,dy);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF) {
int ans = ;
for(int i = ;i <= n;i++)
scanf("%s",g[i]+);
int cnt = ;
for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++) {
if(g[i][j] == 'W') {
ans++;
dfs(i,j);//把所有和该点相邻的W都变成.
}
}
printf("%d\n",ans);
}
return ;
}

bfs:

#include <iostream>
#include <cstdio>
#include <queue>
using namespace std; int n,m;
char G[][];
int dir[][] = {{-,-},{-,},{-,},{,-},{,},{,-},{,},{,}};
typedef pair<int,int> node; void bfs(int x,int y)
{
G[x][y] ='.';//比较好的一种处理方法,省去开vis数组
priority_queue<node> q;//q中存储了(x,y)点的所有连通点
q.push(make_pair(x,y));
while(!q.empty())
{
node t = q.top();
q.pop();
int tx = t.first;
int ty = t.second;
for(int i = ;i < ;i++)
{
int dx = tx+dir[i][];
int dy = ty+dir[i][];
if(dx>=&&dx<=n&&dy>=&&dy<=m && G[dx][dy]=='W') {
G[dx][dy] = '.';
q.push(make_pair(dx,dy));
}
}
}
} int main()
{
while(cin>>n>>m)
{
int ans = ;
for(int i = ;i <= n;i++)
scanf("%s",&G[i][]); for(int i = ;i <= n;i++)
for(int j = ;j <= m;j++)
if(G[i][j]=='W') {
bfs(i,j);
ans++;
}
cout<<ans<<endl;
}
return ;
}

POJ-2386(深广搜基础)的更多相关文章

  1. POJ 2386 Lake Counting (简单深搜)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  2. poj 2386:Lake Counting(简单DFS深搜)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 18201   Accepted: 9192 De ...

  3. POJ 2386 Lake Counting(深搜)

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

  4. POJ 2386 DFS深搜入门

    题目链接 Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, water has pooled in va ...

  5. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

  6. POJ 1190(深搜)

    http://poj.org/problem?id=1190 又有好久没做搜索的题了,没想到做一个卡了我那么久,想哭啊. 一个中文题,思路呢也就是搜索呗,一层一层往上面搜,不过这里有两个比较重要的地方 ...

  7. POJ 2386

    http://poj.org/problem?id=2386 这个题目与那个POJ 1562几乎是差不多的,只不过那个比这个输入要复杂一些 #include <stdio.h> #incl ...

  8. poj - 2386 Lake Counting && hdoj -1241Oil Deposits (简单dfs)

    http://poj.org/problem?id=2386 http://acm.hdu.edu.cn/showproblem.php?pid=1241 求有多少个连通子图.复杂度都是O(n*m). ...

  9. DFS----Lake Counting (poj 2386)

    Lake Counting(POJ No.2386) Description Due to recent rains, water has pooled in various places in Fa ...

随机推荐

  1. CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡【转】

    CentOS 6.3下部署LVS(NAT)+keepalived实现高性能高可用负载均衡   一.简介 VS/NAT原理图: 二.系统环境 实验拓扑: 系统平台:CentOS 6.3 Kernel:2 ...

  2. apk文件伪装zip64格式案例

    软件样本:http://files.cnblogs.com/files/mmmmar/FMRMemoryCleaner.apk 在论坛看在网友求助把一个小的app去广告,下载一看是清理内存的,刚开始让 ...

  3. [转] 智能指针(三):unique_ptr使用简介

    PS: 1. auto_ptr太不安全,可能多个auto_ptr指向一个对象,出现重复释放的问题 2. unique_ptr解决了这个问题,不允许拷贝构造函数和赋值操作符,但是!它支持移动构造函数,通 ...

  4. php开发中将远程图片本地化的方法

    检查文本内容中的远程图片,下载远程图片到本地的方法示例. /** * 下载远程图片到本地 * * @param string $txt 用户输入的文字,可能包含有图片的url * @param str ...

  5. Spring MVC中使用Mongodb总结

    近期项目做了次架构调整,原来是使用MySQL+GeoHash来存储LBS数据(地理位置信息),现在使用NOSQL数据库MongoDB来存储LBS数据(地理位置信息).由于项目是基于spring MVC ...

  6. Android项目中如何用好构建神器Gradle?(转)

    最近在忙团队并行开发的事情,主要是将各个团队的代码分库,一方面可以降低耦合,为后面模块插件化做铺垫,另一方面采用二进制编译,可以加快编译速度.分库遇到了一些问题,很多都要通过Gradle脚本解决,所以 ...

  7. HTML5中class选择器属性的解释

    设置有class属性值的元素,可以被css中的选择器调用,也可以在javascript中以getElementsByClassName()方法调用. 可以给各个元素添加class而且名称可以相同与id ...

  8. CSS中伪类的使用

    原文:http://www.cnblogs.com/guopei/archive/2011/04/16/2017627.html 何为伪类? 也就是实际实现了类的效果,但是并没有实际添加到标签中的类, ...

  9. rest简单实例

    http://www.cnblogs.com/fredric/archive/2012/03/03/2378680.html http://www.thinksaas.cn/topics/0/153/ ...

  10. vs2013+sql server2012

    学习 vs2013 sql server2012使用 : linp lambda entity framework wcf tfs;