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 ...
随机推荐
- HTML5中标记与特殊属性
不允许写结束标记的元素有(只允许<元素/>): area.base.br.col.command.embed.hr.img.input. keygen.link.meta.param.so ...
- 后台dubug有值且sql也打印出来执行了但是前台就是查不到数据
记录在sturts2里面 摔得最深的一次 public String queryJoinAccount(){ //用来存储分页的数据 pageMap=new HashMap<String, Ob ...
- DEV 第三方控件报表分类汇总
最近这段时间难得空闲,于是打算做个报表功能,主要实现数据的分类汇总,以便不时之需.首先看看效果: 主要是根据工程类型这个字段进行分类,每个分类下对应的项目金额进行求和,当然,你也可以根据实际需求,进行 ...
- unity3d贴图2D
在GUI上绘制图片步骤如下: 1.定义一个2D图片纹理变量: public Texture2D pic; 2.关联变量和贴图的关系: 在布局界面选中MainCamera,找到右侧属性列表中的pic选项 ...
- 用GDB命令PO(print-object)打印UIView的视图层级
UIView有一个私有方法: recursiveDescription 这个方法可以显示出当前视图的详细层级,可以在代码中直接调用,也可以在GDB中调用,在GDB中调用时需要借助另一个GDB命令:pr ...
- 弹出键盘windowsoftinputmode属性设置值
windowSoftInputMode属性设置值 2012-08-30 16:49 1592人阅读 评论(0) 收藏 举报 androidattributes活动 (1).AndroidManifes ...
- C++设计模式之适配器模式(三)
4.适配器模式总结 在对象适配器模式中.适配器与适配者之间是关联关系:在类适配器模式中.适配器与适配者之间是继承关系.不论是对象适配器还是类适配器.适配器模式都将现有接口转化为客户类所期望的接口.实现 ...
- python-创建一个本地txt文本
def text_create(name, msg): desktop_path = '/Users/Hou/Desktop/' full_path = desktop_path + name + ' ...
- 现在的C语言编辑器里的int范围为什么是-2147483648~2147483647 2014-08-05 10:21 100人阅读 评论(0) 收藏
下面是引用百度文库的一段话: "这得从二进制的原码说起: 如果以最高位为符号位,二进制原码最大为0111111111111111=215-1=32767 最小为111111111111111 ...
- AssionShop开源B2C电子商务系统-(一)用例图(转载)
这篇文章,将有实质的进展.先把大体的需求整理了一份用例图,自认为粒度做的已经很细了,再细就没法搞了.我还是坚信一个原则:自己业余搞的东西千万不要 想一口吃个胖子,这样很容易项目就搞流产了~,废话不多说 ...