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 , ...
随机推荐
- Android 中状态栏、标题栏、View的大小及区分
1.获得状态栏的高度(状态栏相对Window的位置): Rect frame = new Rect(); getWindow().getDecorView().getWindowVisibleDisp ...
- Android Studio调试工具总结
前言:写代码不可避免有Bug.通常情况下除了日志最直接的调试手段就是debug.当我们的程序出现bug时,调试能够高速的找到bug. 进入调试状态.我们能够清晰的了解程序的整个运行过程,能够对内 ...
- Ural 2018The Debut Album(DP)
题目地址:Ural 2018 简单DP.用滚动数组. 代码例如以下: #include <iostream> #include <cstdio> #include <st ...
- Win7安装软件,界面上中文显示乱码的解决方案
“Control panel”->"Clock,Language and Region"->"Region and Language"->第四 ...
- 百科知识 .e,.ec文件如何打开
1 .e是易语言源文件,你可以从以下网址下载e语言编程环境: http://www.xiazaiba.com/html/409.html 2 安装之后会自动关联.e文件. 3 打开一个e语言文 ...
- 谈一次Linux的木马攻击数据爆满造成的Mysql无法启动
起初以为是mysql它们之间的扩展没有开启! 后来发现,木马的确使它初始化了,最开始没有用图形化界面 而后,修改并且开启所有pdo扩展 VIM基本操作(除了插入,其它的命令前提是按ESC): 插入: ...
- hql 时间
1.hql中时间格式转换 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String d ...
- c#列表操作
Enumerable[从元数据] // // 摘要: // 从序列的开头返回指定数量的连续元素. // // 参数: ...
- man gitworkflows
gitworkflows(7) Manual Page NAME gitworkflows - An overview of recommended workflows with Git SYNOPS ...
- VCC/AVCC/VDD/AVDD区别
V*与AV*的区别是:数字与模拟的区别CC与DD的区别是:供电电压与工作电压的区别(通常VCC>VDD): 数字电路供电VCC 模拟电路供电AVCCVDD是指工作电压,就是供电进芯片的 AVDD ...