POJ 2386 Lake Counting 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了。
由于能够深搜而不用回溯。故此效率就是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 搜索题解的更多相关文章
- POJ 2386 Lake Counting(搜索联通块)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 48370 Accepted: 23775 Descr ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- POJ 2386 Lake Counting
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 28966 Accepted: 14505 D ...
- [POJ 2386] Lake Counting(DFS)
Lake Counting Description Due to recent rains, water has pooled in various places in Farmer John's f ...
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- POJ 2386 Lake Counting 题解《挑战程序设计竞赛》
地址 http://poj.org/problem?id=2386 <挑战程序设计竞赛>习题 题目描述Description Due to recent rains, water has ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
随机推荐
- HTTP===如何理解网关
首先举个例子: 假设你的名字叫小不点(很小),你住在一个大院子里,你的邻居有很多小伙伴,父母是你的网关.当你想跟院子里的某个小伙伴玩,只要你在院子里大喊一声他的名字,他听到了就会回应你,并且跑出来跟你 ...
- malloc和new的区别 end
3. c++中new的几种用法 c++中,new的用法很灵活,这里进行了简单的总结: 1. new() 分配这种类型的一个大小的内存空间,并以括号中的值来初始化这个变量; 2. new[] 分配这种类 ...
- phpstorm+xdebug详解
1.run->edit configurations StartUrl最好是网址,不然容易出错,Server选择的是配置时添加的Servers,详可参考:http://www.cnblogs.c ...
- Ajax 入门笔记
AJAX =Asynchronous Javascript + XML,是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. 通过在后台与服务器进行少量数据交换,AJAX 可以使网页实现异步 ...
- 【C++】继承中的隐藏与覆盖
没有访问控制符时默认为私有继承. 当基类中的某个函数有若干个重载版本,继承类中也实现了该函数的某个重载版本时,参数完全相同的基类版本被覆盖,基类的其他版本被隐藏. 1.若要在继承类中使用基类的被覆盖方 ...
- k8s创建资源的两种方式
命令 vs 配置文件 Kubernetes 支持两种方式创建资源: 1. 用 kubectl 命令直接创建 kubectl run nginx-deployment --image=nginx: -- ...
- rest_framework 访问频率(节流)流程
访问频率流程 访问频率流程与认证流程非常相似,只是后续操作稍有不同 当用发出请求时 首先执行dispatch函数,当执行当第二部时: #2.处理版本信息 处理认证信息 处理权限信息 对用户的访问频率进 ...
- Ajaxterm + nginx 实现一个简单的堡垒机
https://blog.csdn.net/zhang19771105/article/details/50497581 http://wuliangxx.iteye.com/blog/600113
- AC日记——[JSOI2007]建筑抢修 bzoj 1029
1029 思路: 贪心,而且,stl水过: 然而神特么输出que.size()就错! 代码: #include <queue> #include <cstdio> #inclu ...
- uiautomator2 +Python进行Android原生应用UI测试
uiautomator2封装了google的uiautomator(只能用java),uiautomator2可以使用脚本语言python进行编写,更简单直观地修改.运行自动化测试代码: 不足为:仅支 ...