poj 2386:Lake Counting(简单DFS深搜)
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 18201 | Accepted: 9192 |
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
#include <iostream> using namespace std;
int n,m;
char a[][];
int dx[] = {,,,,,-,-,-}; //八个方向
int dy[] = {,,,-,-,-,,};
bool judge(int x,int y)
{
if(x< || x>n || y< || y>m)
return ;
if(a[x][y]!='W')
return ;
return ;
}
void dfs(int x,int y)
{
a[x][y] = '.'; //将‘W’转化为‘.’
for(int i=;i<;i++){
int nx = x + dx[i];
int ny = y + dy[i];
//如果这一步是‘W’,且没有越界,可以走。
if(judge(nx,ny))
continue;
dfs(nx,ny);
}
}
int main()
{
while(cin>>n>>m){
int sum = ;
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
cin>>a[i][j];
for(int i=;i<=n;i++)
for(int j=;j<=m;j++)
if(a[i][j]=='W'){
sum++;
dfs(i,j);
}
cout<<sum<<endl;
}
return ;
}
Freecode : www.cnblogs.com/yym2013
poj 2386:Lake Counting(简单DFS深搜)的更多相关文章
- POJ:2386 Lake Counting(dfs)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40370 Accepted: 20015 D ...
- 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 ...
- (Relax DFS专题1.2)POJ 2386 Lake Counting(使用DFS来计算有多少坨东西是连通的)
题目大意:有N*M的矩阵稻田,'W'表示有积水的地方, '.'表示是干旱的地方,问稻田内一共有多少块积水,根据样例很容易得出,积水是8个方向任一方向相连即可. 题目大意:有N*M的矩阵稻田,'W'表示 ...
- 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: 17917 Accepted: 906 ...
- [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(搜索联通块)
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 ...
随机推荐
- UISegmentedControl 修改字体大小 和 颜色
UISegmentedControl 修改字体大小 和 颜色 大小: UIFont *font = [UIFont boldSystemFontOfSize:14.0f]; NSDictionary ...
- selenium-Getting Started
1.1. Simple Usage If you have installed Selenium Python bindings, you can start using it from Python ...
- 采集音频和摄像头视频并实时H264编码及AAC编码
转自:http://www.cnblogs.com/haibindev/archive/2011/11/10/2244442.html 0. 前言 我在前两篇文章中写了DirectShow捕获音视频然 ...
- 数据库对m³等特殊符号的支持
目前我只遇到过m³这个特殊符号,会影响正常使用. 比如,我在数据库中搜索: select * from table where container='10m³'; 即使数据库中对应的值,但也无法搜索出 ...
- oracle聚合函数及行专列,pivot rollup cube
1.原始数据 --方法-: --以单位分组,计算每类特殊情况的合计以及按照单位的小计数 with a as (SELECT b.szfz, case when tsqk is not null th ...
- Chisel Tutorial(六)——port
下面内容根据2015-7-10版的Chisel 2.2 Tutorial整理 port就是硬件单元对外的接口,须要指明方向(输入还是输出).一个port声明的样例例如以下: class Decoupl ...
- Android新手入门2016(10)--GridView
本文来自肥宝传说之路.引用必须注明出处! GridView跟ListView一样是多控件布局.实现九宫图是最方便的. 还是先看看图,没图说个鸡鸡是不是 如上图.是一种应用方式.在每一个格子里面.放入应 ...
- mysql-group-replication 测试环境的搭建与排错
mysql-group-replication 是由mysql-5.7.17这个版本提供的强一致的高可用集群解决方案 1.环境规划 主机ip 主机名 172.16.192.201 balm001 17 ...
- gdi软光栅化注意事项
1,opengl viewport原点在左下角,而gdi画图api原点在左上角,所以在实现了整个opengl管线,最后将点通过gdi函数画到屏幕时要进行临时转化. 2,注意gdi画点的api传入的颜色 ...
- Centos系统安装JDK详细图文教程
1.查询系统默认JDK Centos系统默认会安装OpenJDK,一般建议是安装sun公司的JDK.我们首先检查系统是否安装有jdk并且是OpenJDK版本的,若是,则将它卸载掉并安装上sun公司的j ...