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 ...
随机推荐
- HLG 2163 方格取数 (最大网络流)
题目链接: m=ProblemSet&a=showProblem&problem_id=2163">点击打开链接 Description : 给你一个n*n的格子的棋 ...
- poj 3278 Catch That Cow (bfs搜索)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46715 Accepted: 14673 ...
- windows 7多点触摸开发
win7 触摸屏系统应用广泛,软件操作方便,功能强大,现以被很多硬件厂商应用. 我曾用一台装有win7 的汉王平板电脑进行了多点触摸软件的开发. 开发环境及条件: 1. 平板电脑+ win7 ...
- POJ 2365 Rope(水题)
[题意简述]:给出我们钉子个数与半径,让我们求出缠绕在钉子上的绳子有多长. [分析]:从题目中我们能够看出,绳子长度的和等于每两个钉子的距离的和加上接触在钉子上的绳子的长度,不难发现这部分长度事实上就 ...
- GOJ1150(矩阵快速幂)
sum Time Limit: 1000ms Problem Description: 给定a和n,计算a+aa+aaa+aaaa+...+a...a(n个a) 的和. Input: 测试数据有多组, ...
- Java LinkedBlockingQueue和ArrayBlockingQueue分析
LinkedBlockingQueue是一个链表实现的堵塞队列,在链表一头增加元素,假设队列满.就会堵塞.还有一头取出元素.假设队列为空.就会堵塞. LinkedBlockingQueue内部使用Re ...
- POJ2155:Matrix(二维树状数组,经典)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...
- C#应用Newtonsoft.Json.dll,控制json的时间格式
原文:C#应用Newtonsoft.Json.dll,控制json的时间格式 var aIsoDateTimeConverter = new IsoDateTimeConverter();aIsoDa ...
- 9个杀手级 JVM 编程语言
9个杀手级 JVM 编程语言 Java虚拟机已经不再是仅仅局限在 Java 了,很多语言提供了脚本转换,可以让其他的程序在java虚拟机上运行,这样能够让更多的开发者能够依靠JVM在Java平台上大有 ...
- hdu1011(树形dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1011 题意:有n个洞组成一棵树,你有m个士兵,你从1号房间开始攻打,每个洞有a个"bugs& ...