poj2386Lake Counting
题意是这种。给你一个N*M的矩形图。这个图由两个东西组成。'.'和'W',
W代表这个地方有水。
.代表这个地方没水。
假设一个有水的地方它的上下左右,左上,坐下。右上。右下也有水,那么
就看成它们连成一块,连在一起的有水的地方看成一个水洼。
如今告诉你N和M以及这个矩形图。要你推断图中有多少个水洼。
我是用dfs做的。
详细做法是首先找一个有水的点,然后假设这个地方有水。则水洼数+1,
然后从这个点出发。进行dfs,把与它相连的有水的地方都变成没水的地方,
也就是'.'。这种话,最后得出的水洼数就是题意所要求的。
代码例如以下
#include<iostream>
using namespace std;
int row,line;
char map[110][110];
void init()
{
int i,j;
scanf("%d%d",&row,&line);
for(i=0;i<row;i++)
scanf("%s",map[i]);
}
bool isbeyond(int i,int j)
{
if(i<0||i>=row||j<0||j>=line)
return 1;
return 0;
}
void dfs(int i,int j)
{
int x,y;
map[i][j]='.';
for(x=-1;x<2;x++)
for(y=-1;y<2;y++)
if(!isbeyond(i+x,j+y)&&map[i+x][j+y]=='W')
dfs(i+x,j+y);
}
void solve()
{
int sum=0,i,j;
init();
for(i=0;i<row;i++)
for(j=0;j<line;j++)
if(map[i][j]=='W')
{
sum++;
dfs(i,j);
}
printf("%d\n",sum);
}
int main()
{
solve();
}
poj2386Lake Counting的更多相关文章
- POJ-2386Lake Counting,搜索题。。
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, w ...
- POJ2386----Lake Counting
/* 利用宽搜将每块积水填满,添加一个计数器,记下填满几块积水即答案 */ #include<iostream> using namespace std; ][]; ][] = {{-,- ...
- 萌新笔记——Cardinality Estimation算法学习(二)(Linear Counting算法、最大似然估计(MLE))
在上篇,我了解了基数的基本概念,现在进入Linear Counting算法的学习. 理解颇浅,还请大神指点! http://blog.codinglabs.org/articles/algorithm ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- ZOJ3944 People Counting ZOJ3939 The Lucky Week (模拟)
ZOJ3944 People Counting ZOJ3939 The Lucky Week 1.PeopleConting 题意:照片上有很多个人,用矩阵里的字符表示.一个人如下: .O. /|\ ...
- find out the neighbouring max D_value by counting sort in stack
#include <stdio.h> #include <malloc.h> #define MAX_STACK 10 ; // define the node of stac ...
- 1004. Counting Leaves (30)
1004. Counting Leaves (30) A family hierarchy is usually presented by a pedigree tree. Your job is ...
- 6.Counting Point Mutations
Problem Figure 2. The Hamming distance between these two strings is 7. Mismatched symbols are colore ...
- 1.Counting DNA Nucleotides
Problem A string is simply an ordered collection of symbols selected from some alphabet and formed i ...
随机推荐
- php中如何开启GD库
php中开启GD库 在浏览器输入启用wamp下的GD库(否则验证码可能不能用) D:\lamp\php\php.ini 文件
- python大文件迭代器的流式读取,之前一直使用readlines()对于大文件可以迅速充满内存,之前用法太野蛮暴力,要使用xreadlines或是直接是f,
#!/usr/bin/env python #encoding=utf-8 import codecs count =0L #for line in file("./search_click ...
- JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView
JavaFX2的ListView中的多选没有提供鼠标拖动选择的功能,同时按下Ctrl和Shift后连续的区间选中也不支持,以下代码用于处理这两个问题,细节见代码注释: import com.sun.j ...
- ajaxterm不好还是gateone好
http://pkgs.org/centos-5-rhel-5/epel-i386/Ajaxterm-0.10-8.el5.noarch.rpm.html Web SSH 客户端Ajaxterm安装 ...
- GridView的RowDataBound事件中获取当前行内容的几种方法
1. Cells[x].Txt. 从列单元格的文本值获取.这种方法简单高率,最为常用,但是功能单纯.此法存在几个缺点: (1)无法获取到设置了隐藏属性的数据列的值,所取到的值为“”(空). ...
- C#操作Cookie
/* 创建者:菜刀居士的博客 * 创建日期: 2014年09月02号 * 功能:操作Cookie * */ namespace Net.String.ConsoleApplication { ...
- xcode多target
原文:http://www.codza.com/free-iphone-app-version-from-the-same-xcode-project There are more than 15,0 ...
- SWT中在treeview中显示图片
package com.repositoryclient.treeview; import org.eclipse.jface.resource.ImageDescriptor; import org ...
- java读取XML文件的四种方式
java读取XML文件的四种方式 Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT& ...
- Android学习十九:ContentProvider初步
一.Content Provider基本概念 1.ContentProvider为存储和获取数据提供了统一的接口.ContentProvide对数据进行封装.不用关心数据存储的细节.使用表的形式来组织 ...