简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了。

由于能够深搜而不用回溯。故此效率就是O(N*M)了。

技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个池塘了,P值为真。

搜索过的池塘不要反复搜索,故此,每次走过的池塘都改成其它字母。如'@',或者'#',随便一个都能够。

然后8个方向搜索。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;
const int MAX_N = 101;
char pond[MAX_N][MAX_N];
const char VIS = '@';
int N, M;
bool P; inline bool isLegal(int r, int c)
{
return 0<=r && 0<=c && r<N && c<M && pond[r][c] == 'W';
} void getPond(int r, int c)
{
if (!isLegal(r, c)) return ;
P = true;
pond[r][c] = VIS;
getPond(r+1, c);
getPond(r-1, c);
getPond(r, c+1);
getPond(r, c-1);
getPond(r+1, c+1);
getPond(r+1, c-1);
getPond(r-1, c+1);
getPond(r-1, c-1);//eight direction search
} int main()
{
while (~scanf("%d %d", &N, &M))
{
getchar();
for (int i = 0; i < N; i++)
{
gets(pond[i]);
}
int ans = 0;
for (int i = 0; i < N; i++)
{
for (int j = 0; j < M; j++)
{
P = false;
getPond(i, j);
ans += P;
}
}
printf("%d\n", ans);
}
return 0;
}

POJ 2386 Lake Counting 搜索题解的更多相关文章

  1. POJ 2386 Lake Counting(搜索联通块)

    Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...

  2. POJ 2386 Lake Counting 八方向棋盘搜索

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53301   Accepted: 26062 D ...

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

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

  4. POJ 2386 Lake Counting(深搜)

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

  5. POJ 2386 Lake Counting

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

  6. [POJ 2386] Lake Counting(DFS)

    Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...

  7. POJ:2386 Lake Counting(dfs)

    Lake Counting Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40370   Accepted: 20015 D ...

  8. POJ 2386 Lake Counting 题解《挑战程序设计竞赛》

    地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...

  9. 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)

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

随机推荐

  1. servlet(2) - 利用MyEclipse新建一个servlet - 小易Java笔记

    1.Tomcat在MyEclipse中集成 ==> Window-preferences-MyEclipse-Servers-Tomcat-Tomcat 6.x-点击右侧的Browse,选择你的 ...

  2. go语言实现拷贝文件

    package main import ( "fmt" "io" "os" ) func main(){ list := os.Args / ...

  3. 【Android开发日记】之入门篇(十四)——Button控件+自定义Button控件

        好久不见,又是一个新的学期开始了,为什么我感觉好惆怅啊!这一周也发生了不少事情,节假日放了三天的假(好久没有这么悠闲过了),实习公司那边被组长半强制性的要求去解决一个后台登陆的问题,结果就是把 ...

  4. Centos7Yum安装PHP7.2

    1.安装源 安装php72w,是需要配置额外的yum源地址的,否则会报错不能找到相关软件包. php高版本的yum源地址,有两部分,其中一部分是epel-release,另外一部分来自webtatic ...

  5. win上安装Redis并将其设置为服务

    win上安装Redis并将其设置为服务 redis下载地址:https://redis.io/ 或者在下面的地址下载 https://github.com/zhangxy1035/redisDownl ...

  6. Nodejs获取Azure Active Directory AccessToken

    因为现有的代码已经迁入至Azure中,并且受AD保护,所以在获取数据时,需要传入Token后才可以获取到数据,那么第一步肯定是需要先提取到token var adal = require('adal- ...

  7. Codeforces 723 A. The New Year: Meeting Friends

    A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...

  8. 洛谷P3929 SAC E#1 - 一道神题 Sequence1【枚举】

    题目描述 小强很喜欢数列.有一天,他心血来潮,写下了一个数列. 阿米巴也很喜欢数列.但是他只喜欢其中一种:波动数列. 一个长度为n的波动数列满足对于任何i(1 <= i < n),均有: ...

  9. Cocos2dx-Lua UIScrollView 和 UITableView 对比

    为什么写这个 上面这个问题的答案也是我写这篇文章的初衷,在最近给游戏添加一些列表的时候,对比着应用了一下他们两个,在它们两个之间的优劣势之间进行取舍,就有了这个问题的答案. 按照我一个iOS开发而言, ...

  10. csu1216( Trie )

    csu1216 题意 给定一些数,求这些数中两个数的异或值最大的那个值. 分析 转化成二进制数存入字典树,比如说要查询 \(0011\) ,显然和 \(1100\) 结合最优,所以我们直接在字典树上寻 ...