BZOJ 1619 [Usaco2008 Nov]Guarding the Farm 保卫牧场:dfs【灌水】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1619
题意:
给你一个n*m的地形图,位置(x,y)的海拔为h[x][y]。
一个山顶的定义为:可以是一个点,或是一片海拔相同的区域。
要求为山顶周围(每个点的八个方向)的海拔均比山顶低(或为边界)。
问你有多少个山顶。
题解:
dfs灌水法。
将所有点按海拔从高到低排序。
依次从每个点灌水,水流到的地方做标记。
灌水:如果一个点有水,则它周围海拔比它低的点也会被水淹。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define MAX_N 705 using namespace std; const int dx[]={-,,,,-,-,,};
const int dy[]={,,-,,-,,-,}; struct Coor
{
int x;
int y;
int t;
Coor(int _x,int _y,int _t)
{
x=_x;
y=_y;
t=_t;
}
Coor(){}
friend bool operator < (const Coor &a,const Coor &b)
{
return a.t>b.t;
}
}; int n,m;
int ans=;
int h[MAX_N][MAX_N];
bool vis[MAX_N][MAX_N];
vector<Coor> v; void read()
{
cin>>n>>m;
memset(h,-,sizeof(h));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
cin>>h[i][j];
v.push_back(Coor(i,j,h[i][j]));
}
}
} inline bool is_legal(int x,int y)
{
return x> && x<=n && y> && y<=m && !vis[x][y];
} void dfs(int x,int y)
{
vis[x][y]=true;
for(int i=;i<;i++)
{
int nx=x+dx[i];
int ny=y+dy[i];
if(is_legal(nx,ny))
{
if(h[x][y]>=h[nx][ny])
{
dfs(nx,ny);
}
}
}
} void solve()
{
sort(v.begin(),v.end());
memset(vis,false,sizeof(vis));
for(int i=;i<v.size();i++)
{
Coor now=v[i];
if(!vis[now.x][now.y])
{
dfs(now.x,now.y);
ans++;
}
}
} void print()
{
cout<<ans<<endl;
} int main()
{
read();
solve();
print();
}
BZOJ 1619 [Usaco2008 Nov]Guarding the Farm 保卫牧场:dfs【灌水】的更多相关文章
- BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场
题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec Memory Limit: 64 MB Submit: 491 S ...
- bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场【bfs】
不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!! 是我看不懂人话还是翻译不说人话= = 把所有格子按值排个序,bfs扩展打标记即可 #includ ...
- 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场
1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 498 Solve ...
- 【BZOJ】1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场(dfs)
http://www.lydsy.com/JudgeOnline/problem.php?id=1619 首先不得不说,,题目没看懂.... 原来就是找一个下降的联通块.... 排序后dfs标记即可. ...
- [Usaco2008 Nov]Guarding the Farm 保卫牧场[DFS]
Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...
- bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场
Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...
- BZOJ 1229: [USACO2008 Nov]toy 玩具
BZOJ 1229: [USACO2008 Nov]toy 玩具 标签(空格分隔): OI-BZOJ OI-三分 OI-双端队列 OI-贪心 Time Limit: 10 Sec Memory Lim ...
- BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )
二分一下答案就好了... --------------------------------------------------------------------------------------- ...
- BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛( dp )
状压dp dp( x , S ) 表示最后一个是 x , 当前选的奶牛集合为 S , 则状态转移方程 : dp( x , S ) = Σ dp( i , S - { i } ) ( i ∈ S , ...
随机推荐
- OpenGL - Tessellation Shader 【转】
http://blog.sina.com.cn/s/blog_8c7d49f20102v4qm.html Patch is just an ordered list of vertices (在tes ...
- 怎么设置MySQL就能让别人访问本机的数据库了?
for all ips use below GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' WITH GRANT OPTION; for particular ...
- Java Web Filter登录验证
初做网站需要登录验证,转自 :http://blog.csdn.net/daguanjia11/article/details/48995789 Filter: Filter是服务器端的组件,用来过滤 ...
- fping - send ICMP ECHO_REQUEST packets to network hosts
NAME fping - send ICMP ECHO_REQUEST packets to network hosts SYNOPSIS fping [ options ] [ systems... ...
- Spring里bean之间的循环依赖解决与源码解读
通过前几节的分析,已经成功将bean实例化,但是大家一定要将bean的实例化和完成bean的创建区分开,bean的实例化仅仅是获得了bean的实例,该bean仍在继续创建之中,之后在该bean实例的基 ...
- YUV格式
http://blog.csdn.net/u011270282/article/details/50696616 http://blog.csdn.net/acs713/article/details ...
- 基于UDP的一对回射客户/服务器程序
前言 之前曾经学习过一对回射客户/服务器程序的例子,不过那个是基于TCP协议的.本文将讲解另一对回射客户/服务器程序,该程序基于UDP协议.由于使用的协议不同,因此编写出的程序也有本质上的区别,应将它 ...
- 嵌入式开发之cgic库---cgi库的使用
很幸运!用C语言写CGI程序还可以有比较简单的方式,那就是我们可以借助使用第三方库CGIC(CGIC是一个功能比较强大的支持CGI开发的标准C库,并支持Linux, Unix 和Windows等多操作 ...
- 图像处理之图像增强项目---csdn去雾专栏1
(一)高斯低通滤波去噪 高斯低通滤波器(Gaussian Low Pass Filter)是一类传递函数为高斯函数的线性平滑滤波器.又由于高斯函数是正态分布的密度函数.因此高斯低通滤波器对于去除服从正 ...
- 在zend framework框架中try{}catch(Exception e){}的跳转问题
请勿盗版,转载请加上出处http://blog.csdn.net/yanlintao1 首先我先说明我遇到的问题 try{ //导入学生信息 $ModelStudent->insert($dat ...