POJ 2386 Lake Counting(深搜)
Lake Counting
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17917 Accepted: 9069
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
思路
从任意的W开始,不停地把邻接的部分用'.'代替。 1次DFS后与初始的这个W连接的所有W就都被替
换成了'.',因此直到图中不再存在W为止,总共进行DFS的次数就是答案了。 8个方向共对应了8种
状态转移,每个格子作为DFS的参数至多被调用一次,所以复杂度为O(8×N×M)=O(N×M)。
#include<stdio.h>
#define MAX_N 105
#define MAX_M 105
char field[MAX_N][MAX_M];
int N,M;
void dfs(int x,int y)
{
int dx,dy,nx,ny;
field[x][y] = '.'; //将现在位置替换为'.';
for (dx = -1;dx < 2;dx++) //遍历移动的8个方向
{
for (dy = -1;dy < 2;dy++)
{
nx = x + dx,ny = y + dy;
if (0 <= nx && nx < N && 0 <= ny && ny < M && field[nx][ny] == 'W')
{ //判断(nx,ny)是不是在园子内
dfs(nx,ny);
}
}
}
}
int main()
{
int i,j,res = 0;
char tmp;
scanf("%d%d",&N,&M);
getchar();
for (i=0;i<N;i++)
{
for (j=0;j<M;j++)
{
scanf("%c",&field[i][j]);
if (j==M-1)
{
scanf("%c",&tmp);
}
}
}
for (i = 0;i < N;i++)
{
for (j = 0;j < M;j++)
{
if (field[i][j] == 'W')
{
dfs(i,j);
res++;
}
}
}
printf("%d\n",res);
return 0;
}
POJ 2386 Lake Counting(深搜)的更多相关文章
- poj 2386:Lake Counting(简单DFS深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 18201 Accepted: 9192 De ...
- POJ 2386 Lake Counting (简单深搜)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 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: 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 搜索题解
简单的深度搜索就能够了,看见有人说什么使用并查集,那简直是大算法小用了. 由于能够深搜而不用回溯.故此效率就是O(N*M)了. 技巧就是添加一个标志P,每次搜索到池塘,即有W字母,那么就觉得搜索到一个 ...
- POJ 2386 Lake Counting 八方向棋盘搜索
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53301 Accepted: 26062 D ...
- 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). ...
随机推荐
- 简单谈谈dom解析xml和html
前言 文件对象模型(Document Object Model,简称DOM),是W3C组织推荐的处理可扩展标志语言的标准编程接口.html,xml都是基于这个模型构造的.这也是一个W3C推出的标准.j ...
- Java 基础【11】@注解
1.注解简介 JDK 1.5 中引入的 java.lang.annotation 包提供注解编程支持,可以让类在编译.类加载.运行时被读取,并执行相应的处理. 在 Java EE应用的时候,总是免不了 ...
- 如何对于几百行SQL语句进行优化?
1.最近在开发中遇到的一些关于几百行SQL语句做查询的问题,需要如何的解决优化SQL这确实是个问题,对于当下的ORM 框架 EF 以及其他的一些的开源的框架例如Drapper ,以及Sqlite-Su ...
- 让 Generator 自启动
文章同步自个人博客:http://www.52cik.com/2016/07/11/generator-co.html 此前只是简单使用而没有真正的去研究 Generator,这次要好好折腾下这货. ...
- APP架子迁移指南(二)
接上一篇,这一篇开始用android来解释MVP概念.八股式的架子结构和命名规范.我在准备这篇文章的时候还看到不少在MVP基础上衍生的架子思路,底子是MVP没错,但命名有区别.复杂度变了.架子也用到了 ...
- 10-xargs 简明笔记
从标准你输入获取内容创建和执行命令 xargs [options] 选项 -n 数字,分组 示例 x ...
- Yii2 使用小部件 Breadcrumbs
yii有两种Breadcrumbs写法,one: echo Breadcrumbs::widget([ 'itemTemplate' => "<li><i>{l ...
- window php redis扩展下载地址
redis扩展下载 http://windows.php.net/downloads/pecl/snaps/redis/
- javascript生成GUID的代码
<script type="text/javascript"> var Guid = function(){}; Guid.prototype = { S4:funct ...
- MyBatis学习--mybatis开发dao的方法
简介 使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法. 主要概念介绍: MyBatis中进行Dao开发时候有几个重要的类,它们是SqlSessionFac ...