题目链接: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. Maven修改默认本地资源库文件夹

    默认的Maven地址如下: Linux/Mac:~/.m2(提示:~/为当前用户目录地址) Widnows:C:\Users\{username}\.m2(提示:username为当前用户名) 修改操 ...

  2. 手动安装windows的磁盘清理工具

    All you really need to do is copy some files that are already located on your server into specific s ...

  3. iOS -- 原生NSStream实现socket

    - (void)startSocket:(NSString *)address andPort:(int)port { CFReadStreamRef readRef; CFWriteStreamRe ...

  4. 拦截recyclerview 的item 的点击事件

    recyclerview.addOnItemTouchListener(new RecyclerItemClickListener(getActivity(),recyclerview, new Re ...

  5. 每天进步一点点—SQL优化

    一.           SQL优化 1.   通过show status 命令了解各种SQL的运行频率 mysql>show status like 'Com_%'; +----------- ...

  6. oralce中相关的概念整理

    [数据库名]  概念:就是一个数据库的标识,作用等同于我们的身份证的作用,假设一台机器上安装了多个数据库,那么每一个数据库都会有一个数据库名称相应,这些数据库名称在数据库被创建的时候,数据库名称也会被 ...

  7. 渗透测试思路 | Linux下自动化搭建FakeAP,劫持用户在Portal认证下的所有流量

    如何在linux下搭建一个fakeap,使得portal认证下的用户无法发现连接你的假AP,并且能够正常上网.先说一下portal认证.无线WIFI认证方式主要有wpa2 和 open两种,而port ...

  8. linux 中两个文档怎么对比内容是否一致

    可以用diff命令对比文档内容.[语法]: diff [参数] 文件1 文件2[说明]: 本命令比较两个文本文件,将不同的行列出来-b 将一串空格或TAB 转换成一个空格或TAB-e 生成一个编辑角本 ...

  9. hdoj 4828 卡特兰数取模

    Grids Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Sub ...

  10. 安装惠普M1136打印机一直处于“新设备已连接”状态怎么办?

    百度的答案是从控制面板的添加打印机入手,我试了遇到找不到设备的问题. 其实问题的原因是在安装驱动时一直把打印机到电脑的USB插着.我的解决方案是: 1.点击M1130MFP_M1210MFP开始安装, ...