题目链接: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【灌水】的更多相关文章

  1. BZOJ 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    题目 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 491  S ...

  2. bzoj 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场【bfs】

    不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!!不是严格小于是小于等于啊!!!!! 是我看不懂人话还是翻译不说人话= = 把所有格子按值排个序,bfs扩展打标记即可 #includ ...

  3. 1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场

    1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 498  Solve ...

  4. 【BZOJ】1619: [Usaco2008 Nov]Guarding the Farm 保卫牧场(dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1619 首先不得不说,,题目没看懂.... 原来就是找一个下降的联通块.... 排序后dfs标记即可. ...

  5. [Usaco2008 Nov]Guarding the Farm 保卫牧场[DFS]

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

  6. bzoj1619[Usaco2008 Nov]Guarding the Farm 保卫牧场

    Description The farm has many hills upon which Farmer John would like to place guards to ensure the ...

  7. BZOJ 1229: [USACO2008 Nov]toy 玩具

    BZOJ 1229: [USACO2008 Nov]toy 玩具 标签(空格分隔): OI-BZOJ OI-三分 OI-双端队列 OI-贪心 Time Limit: 10 Sec Memory Lim ...

  8. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )

    二分一下答案就好了... --------------------------------------------------------------------------------------- ...

  9. BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛( dp )

    状压dp dp( x , S ) 表示最后一个是 x , 当前选的奶牛集合为 S , 则状态转移方程 : dp( x , S ) =  Σ dp( i , S - { i } )  ( i ∈ S , ...

随机推荐

  1. Chrom查看Flash缓存文件及Flash下载方法

    比如在优酷看视频时,或者熊猫直播,如果使用Flash进行播放的基本都会先缓存在本地,只不过这个缓存的名字后缀不叫flv,而是类似tmp这样:通常只要找到这个缓存文件,然后改为flv即可播放:如果出现文 ...

  2. 高性能内存池NedAlloc

    http://www.nedprod.com/programs/portable/nedmalloc/ http://blog.sina.com.cn/s/blog_6f5b220601012x4t. ...

  3. 【CSS】获取元素的z-index值以及各种值的意义

    js可以获取其元素的z-index值: $("document").ready(function(){ var a = $('.row').css('z-index'); aler ...

  4. 牛人写的facebook优化php来龙去脉

    背景 HHVM 是 Facebook 开发的高性能 PHP 虚拟机,宣称比官方的快9倍,我很好奇,于是抽空简单了解了一下,并整理出这篇文章,希望能回答清楚两方面的问题: HHVM 到底靠谱么?是否可以 ...

  5. tensorflow提示出错'module' object has no attribute 'pack'

    编译旧的代码,会像下面这样提示出错: deconv_shape3 = tf.pack([shape[0], shape[1], shape[2], NUM_OF_CLASSESS]) Attribut ...

  6. 4.php整合Memcached

    用法: nginx响应请求时,直接请求memcached, 如果没有相应的内容,再回调PHP页面,去查询database,并写入memcached. 分析: memcached是k/v存储, key- ...

  7. C语言-回溯例3

    排列问题 1.实现排列A(n,m)对指定的正整数m,n(约定1<m<=n),具体实现排列A(n,m).2. 回溯算法设计设置一维数组a,a(i)(i=1,2,…,m)在1—n中取值.首先从 ...

  8. 【Sprint3冲刺之前】TDzhushou软件项目测试计划书

    TDzhushou软件测试计划文档 文档编号:2014-5-8 产品版本:1.1 产品名称:TDzhushou 文 档 作 者: 解凤娇       日期:2014-5-4 软件测试计划 目录 第一章 ...

  9. 不能hadoop-daemon.sh start datanode, 显示 错误: 找不到或无法加载主类 ”-Djava.library.path=.home.hadoop.apps.hadoop-2.6.4.lib”

    这两行代码是用来解决一个Hadoop,32位和64位不兼容的警告的,(这个警告可以忽略) 这两行加到mini2~min4后, export HADOOP_COMMON_LIB_NATIVE_DIR=$ ...

  10. Linux学习笔记——例说makefile 综合案例

    0.前言     从学习C语言開始就慢慢開始接触makefile,查阅了非常多的makefile的资料但总感觉没有真正掌握makefile,假设自己动手写一个makefile总认为非常吃力.所以特意借 ...