POJ:2386 Lake Counting(dfs)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 40370 | Accepted: 20015 |
Description
Given a diagram of Farmer John's field, determine how many ponds he has.
Input
* 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
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
Hint
There are three ponds: one in the upper left, one in the lower left,and one along the right side.
Source
题意:n行m列中,连在一起没有分开的W块有多少
从任意的W开始,不停地把邻接的部分用 . 代替。一次dfs后与初始的这个W连接的所有W就都被替换成了 . 。因此直到图中不再存在W为止,总共进行的dfs次数就是答案。
8个方向共对应了8种状态转移,每个格子作为dfs的参数至多被调用一次,复杂度为O(8*N*M)=O(N*M)
AC代码:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
using namespace std;
int n,m;
const int maxn=1e3+10;
char ch[maxn][maxn];
void dfs(int x,int y)
{
ch[x][y]='.';
for(int dy=-1;dy<=1;dy++)
for(int dx=-1;dx<=1;dx++)
{
int nx=x+dx,ny=y+dy;
if(nx>=0&&nx<n&&ny>=0&&ny<m&&ch[nx][ny]=='W') dfs(nx,ny);//不断递归,把n*m区域内的W变成.
}
}
int main()
{
cin>>n>>m;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
cin>>ch[i][j];
int sum=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(ch[i][j]=='W')
{
dfs(i,j);
sum++;
}
}
cout<<sum<<endl;
return 0;
}
POJ:2386 Lake Counting(dfs)的更多相关文章
- POJ 2386——Lake Counting(DFS)
链接:http://poj.org/problem?id=2386 题解 #include<cstdio> #include<stack> using namespace st ...
- 题解报告:poj 2386 Lake Counting(dfs求最大连通块的个数)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- 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: 48370 Accepted: 23775 Descr ...
- POJ 2386 Lake Counting(深搜)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17917 Accepted: 906 ...
- Poj2386 Lake Counting (DFS)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 49414 Accepted: 24273 D ...
- POJ 2386 Lake Counting (简单深搜)
Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...
- [USACO10OCT]Lake Counting(DFS)
很水的DFS. 为什么放上来主要是为了让自己的博客有一道DFS题解,,, #include<bits/stdc++.h> using namespace std; ][],ans,flag ...
- (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...
随机推荐
- 常用 对象检测 api
isPrototypeOf() 判断某个 proptotype 对象和某个实例之间的关系 alert(Cat.prototype.isPrototypeOf(cat1)); //true ale ...
- 【Golang 接口自动化07】struct转map的三种方式
背景 我们在前面介绍过怎么使用net/http发送json或者map数据,那么它能不能直接发送结构体数据呢?我们今天一起来学习结构体struct转map的三种方法,为后续做铺垫. struct转map ...
- OpenGL入门程序一:绘制简单的矩形
#include <GL/glut.h> void MyDisplay(void); int main(int argc, char **argv) { //设置窗口的大小 glutIni ...
- 雷林鹏分享:C# 运算符重载
C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的.与其 ...
- ArcGIS API for Silverlight/WPF 2.1学习笔记(一)——精简版
一.安装 1.Visual Studio: (1)Visual Studio 2010或Visual Web Developer Express 2010 (2)Silverlight 4 Tools ...
- LeetCode--226--翻转二叉树
问题描述: 翻转一棵二叉树. 示例: 输入: 4 / \ 2 7 / \ / \ 1 3 6 9 输出: 4 / \ 7 2 / \ / \ 9 6 3 1 备注: 这个问题是受到 Max Howel ...
- python-day64--web框架
http协议. 一.HTTP简介 1.HTTP协议是Hyper Text Transfer Protocol(超文本传输协议)的缩写,是用于从万维网(WWW:World Wide Web )服务器传输 ...
- html5阴影
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> < ...
- 小程序数组型图片自适应效果的实现(交流QQ群:604788754)
//本例代码如有问题,请加群,下载今日日期文件,测试.(如对本例有疑问,也可加群咨询群主) WXML: <view class="imgbox"> <block ...
- UVALive 5846 计数
DES:给出任意两点之间连线的颜色.问一共会有多少个由相同颜色的边组成的三角形. 一共有C(3, n)个三角形.考虑一每个点为顶点的三角形.颜色不同的两条边组成的三角形一定不行.所以减去. 题目链接: ...