题目链接: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. PHP平均整数红包算法

    <?php function RandomMoney( $money,$num ){ $arr = array(); $total_money = 0; $this_money = $money ...

  2. 数字类型(NSInteger,NSUInteger,NSNumber)

    在Objective-C中,我们可以使用c中的数字数据类型,int.float.long等.它们都是基本数据类型,而不是对象.也就是说,不能够向它们发送消息.然后,有些时候需要将这些值作为对象使用. ...

  3. LINUX6.5 + MYSQL5.6 + JIRA6.3 + CONFLUENCE5.9

    #================MYSQL=============================    [root@localhost Desktop]# /sbin/ifup eth0[roo ...

  4. BEGINNING SHAREPOINT&#174; 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers 远程Event Receivers App级别生命周期

    BEGINNING SHAREPOINT® 2013 DEVELOPMENT 第12章节--SP 2013中远程Event Receivers  远程Event Receivers App级别生命周期 ...

  5. 三维场景如何嵌入到PPT中展示?

    今天要跟大家一起交流的大体内容如标题所示,日常生活中,ppt已经成为人们工作学习生活中不可或缺的工具之一,那么三维场景是如何在ppt中加载展示的呢?请大家慢慢往下看. 1.创建命令按钮和web bro ...

  6. [Algorithms] Sort an Array with a Nested for Loop using Insertion Sort in JavaScript

    nsertion sort is another sorting algorithm that closely resembles how we might sort items in the phy ...

  7. java数据库连接池简单实现

    package cn.lmj.utils; import java.io.PrintWriter; import java.lang.reflect.InvocationHandler; import ...

  8. Oracle-31-对视图DML操作

    一.对视图进行DML操作 1.创建一个视图v_person create or replace noforceview v_person as select *from person where id ...

  9. c++对象内存模型【内存布局】(转)

    总结:1.按1继承顺序先排布基于每个父类结构.2.该结构包括:基于该父类的虚表.该父类的虚基类表.父类的父类的成员变量.父类的成员变量.3.多重继承且连续继承时,虚函数表按继承顺序排布函数与虚函数.4 ...

  10. caffe搭建--opensuse13.2上搭建caffe开发环境

    第一部分:参考一下内容.将sudo 替换成zypper即可. --------------------------------------------这部分参照以下官网内容-------------- ...