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 ...
随机推荐
- Qt窗口操作函数(最大化,全屏,隐藏最大化,最小化)
Qt窗口中的一些小技术总结 //Qt主窗口没有最小化,最大化按钮且最大化显示 int main(int argc, char *argv[]) { QApplication a(argc, argv ...
- Android Application Fundamentals——Android应用程序基础知识
Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvid ...
- 用jsp写注冊页面
包含单选框.多选框.session的应用,页面自己主动跳转,中文乱码的处理,入门级 对于中文乱码的处理,注意几点:注冊页面数据提交方式为post不能忘了写,页面编码方式为gbk,处理提交信息的doRe ...
- Linux Shell 之 我的第一个Shell程序
这里我首先会介绍一个Shell是什么,再介绍我的第一个Shell程序和从中总结的经验. 一.Shell是什么 在说我的这个Shell程序之前,还是先跟大家说说什么是Shell吧,相信Shell这个 ...
- HttpGet协议与正则表达
使用HttpGet协议与正则表达实现桌面版的糗事百科 写在前面 最近在重温asp.net,找了一本相关的书籍.本书在第一章就讲了,在不使用浏览器的情况下生成一个web请求,获取服务器返回的内容.于 ...
- OCA读书笔记(15) - 执行数据库备份
物理备份 -- 数据文件,控制文件,日志文件,参数文件 数据库备份 冷备 -- 归档和非归档均可以 什么时候必须用冷备?1. 数据库的模式为非归档的2. 用于现场保护 冷备的过程:1. 首先查看备份文 ...
- VMware中linux与window目录共享
在虚拟机下来实如今windows下共享一个目录: (前提已安装完毕vmtools:http://blog.csdn.net/pipisorry/article/details/21318931) 打开 ...
- nginx fastcgi 自定义错误页面
http{ fastcgi_intercept_errors on; error_page 404 /404.html; } fastcgi_intercept_errors on;必须设置 之后通过 ...
- Shell脚本检查memcache进程并自己主动重新启动
修正版: #!/bin/sh #check memcache process and restart if down mm_bin="/usr/local/bin/memcached&quo ...
- zzu--2014年11月16日月潭赛 B称号
1229: Rational Resistance Time Limit: 1 Sec Memory Limit: 128 MB Submit: 8 Solved: 4 [id=1229" ...