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). ...
随机推荐
- Windows下Memcache的安装及PHP扩展配置
一.下载 找到完整的memcache的Windows安装包,解压放在硬盘上,比如 F:\memcached.exe 二.安装 WIN7 64位双击打开这个exe可能只有一个空的窗口,不能输入任何命令, ...
- Centos下编译JDK
因为OpenJDK是开源的,这里使用openJDK进行编译联系 环境要求 Centos6.7 64位 openjdk-7u40-fcs-src-b43-26_aug_2013.zip bootstra ...
- Unix 复制文件至指定目录
cp /gaps/log/20160504/bxdx_20160504.log.Z /home 将/gaps/log/20160504/bxdx_20160504.log.Z 文件复制到home路 ...
- 【CSS】 background
background: #22b4ff //背景色 url("http://images.cnblogs.com/cnblogs_com/oiliu/529256/o_titleIMG.jp ...
- JS日历制作获取时间
1.直接获取 var myDate = new Date(); myDate.getYear(); 获取当前年份(2位) myDate.getFullYear(); 获取完整的年份(4位,1970-? ...
- Intent和Intent Filters
什么是Intent Intent是android开发中的重要对象,它作为一个信息承载对象存在. 我们可以在使用其他一些组件的时候从Intent获取行为响应的准则(即应该做什么东西,如何 ...
- 【Alpha版本】冲刺阶段——Day 4
我说的都队 031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬 ...
- oracle判断字段是否存在语句
declare v_cnt number; begin select count(*) into v_cnt from dba_tab_columns where table_name='T_IDC_ ...
- [转]响应式WEB设计学习(3)—如何改善移动设备网页的性能
原文地址:http://www.jb51.net/web/70362.html 前言 移动设备由于受到带宽.处理器运算速度的限制,因而对网页的性能有更高的要求.究竟是网页中的何种元素拉低了网页在移动设 ...
- javascript 红宝书笔记之操作日期
创建当日 日期对象 调用Date的构造函数而不传递参数的情况下,新创建的对象默认获取当前的日期和时间. var now = new Date(); 创建特定的日期和时间对象 Date. ...