洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm
题目描述
The farm has many hills upon which Farmer John would like to place guards to ensure the safety of his valuable milk-cows.
He wonders how many guards he will need if he wishes to put one on top of each hill. He has a map supplied as a matrix of integers; the matrix has N (1 < N <= 700) rows and M (1 < M <= 700) columns. Each member of the matrix is an altitude H_ij (0 <= H_ij <= 10,000). Help him determine the number of hilltops on the map.
A hilltop is one or more adjacent matrix elements of the same value surrounded exclusively by either the edge of the map or elements with a lower (smaller) altitude. Two different elements are adjacent if the magnitude of difference in their X coordinates is no greater than 1 and the magnitude of differences in their Y coordinates is also no greater than 1.
农场里有许多山丘,在山丘上约翰要设置哨岗来保卫他的价值连城的奶牛.
约翰不知道有多少山丘,也就不知道要设置多少哨岗.他有一张地图,用整数矩阵的方式描 述了农场N(1 <= N<=700)行M(1 < M<=700)列块土地的海拔高度好 H_ij (0 <= H_ij <= 10,000).请帮他 计算山丘的数量.
一个山丘是指某一个方格,与之相邻的方格的海拔高度均严格小于它.当然,与它相邻的方 格可以是上下左右的那四个,也可以是对角线上相邻的四个.
输入输出格式
输入格式:
Line 1: Two space-separated integers: N and M
- Lines 2..N+1: Line i+1 describes row i of the matrix with M
space-separated integers: H_ij
输出格式:
- Line 1: A single integer that specifies the number of hilltops
输入输出样例
8 7 4 3 2 2 1 0 1 3 3 3 2 1 0 1 2 2 2 2 1 0 0 2 1 1 1 1 0 0 1 1 0 0 0 1 0 0 0 0 1 1 1 0 0 1 2 2 1 1 0 0 1 1 1 2 1 0
3
说明
There are three peaks: The one with height 4 on the left top, one of the points with height 2 at the bottom part, and one of the points with height 1 on the right top corner.
贪心+dfs
我们知道山峰为中间高四周低的地方,这样我们寻找一下有没有这样的区域使中间高。我们可以存一下比较高的区域,然后判断每一个高的区域是否已经属于另一座山峰,如果不是,则说明这个地即为下一个山峰的最高点,然后从这个点进行dfs,遍历它周围可以与她组成一个山峰的地方,我们知道这样的地方的高度一定比他低
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 1100
using namespace std;
bool vis[N][N];
int n,m,x,y,h[N][N],ans,sum;
]={,,-,-,-,,,},yy[]={,-,,,-,,,-};
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Node
{
int x,y,h;
}node[N*N];
int cmp(Node a,Node b)
{
return a.h>b.h;
}
void dfs(int x,int y)
{
;i<;i++)
{
int fx=x+xx[i],fy=y+yy[i];
||fy<||fx>n||fy>m) continue;
if(h[fx][fy]<=h[x][y]&&!vis[fx][fy])
{
vis[fx][fy]=true;
dfs(fx,fy);
}
}
return ;
}
int main()
{
n=read(),m=read();
;i<=n;i++)
;j<=m;j++)
{
h[i][j]=read();
node[++sum].x=i;
node[sum].y=j;
node[sum].h=h[i][j];
}
sort(node+,node++sum,cmp);
;i<=sum;i++)
{
x=node[i].x,y=node[i].y;
if(vis[x][y]) continue;
vis[x][y]=true;
ans++; dfs(x,y);
}
printf("%d",ans);
;
}
洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm的更多相关文章
- 洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...
- 洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm
https://www.luogu.org/problem/show?pid=2919 题目描述 The farm has many hills upon which Farmer John woul ...
- bzoj1619 / P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 相似题:P3456 [POI2007]GRZ-Ridges and Valleys 按海拔是否相同分块 每次bfs海拔相 ...
- 【luogu P2919 [USACO08NOV]守护农场Guarding the Farm】 题解
题目链接:https://www.luogu.org/problemnew/show/P2919 1.搜索的时候分清楚全局变量和局部变量的区别 2.排序优化搜索 #include <cstdio ...
- P2919 [USACO08NOV]守护农场Guarding the Farm
链接:P2919 ----------------------------------- 一道非常暴力的搜索题 ----------------------------------- 注意的是,我们要 ...
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm
农夫约翰和他的奶牛准备去旅行,所以约翰想要把他的农场临时关闭. 农场有N个牛棚(牛棚从1到N编号),有M条路连接这些牛棚(1≤N,M≤3000). 约翰打算挨个关闭牛棚,在关牛棚的时候, 他突然想起一 ...
- 洛谷 P2921 [USACO08DEC]在农场万圣节Trick or Treat on the Farm
题目描述 每年,在威斯康星州,奶牛们都会穿上衣服,收集农夫约翰在N(1<=N<=100,000)个牛棚隔间中留下的糖果,以此来庆祝美国秋天的万圣节. 由于牛棚不太大,FJ通过指定奶牛必须遵 ...
- 洛谷P3144 [USACO16OPEN]关闭农场Closing the Farm_Silver
题目描述 Farmer John and his cows are planning to leave town for a long vacation, and so FJ wants to tem ...
- 洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 解题报告
P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows 题意: 给定一个长\(N\)的序列,求满足任意两个相邻元素之间的绝对值之差不超过\(K\)的这个序列的排列有多少个? 范围: ...
随机推荐
- POJ:1753-Flip Game(二进制+bfs)
题目链接:http://poj.org/problem?id=1753 Flip Game Time Limit: 1000MS Memory Limit: 65536K Description Fl ...
- Python虚拟机之异常控制流(四)
Python虚拟机中的异常控制流 先前,我们分别介绍了Python虚拟机之if控制流(一).Python虚拟机之for循环控制流(二)和Python虚拟机之while循环控制结构(三).这一章,我们来 ...
- Ubuntu首次登入与在线求助man page总结
1.为了避免瞬间断电造成的Linux系统的危害,建议做为服务器的Linux主机应该加上不断电系统来持续提供稳定的电力 2.默认的图形模式登入中,可以选择语系以及作业阶段.作业阶段为多种窗口管理员软件所 ...
- doc下设置永久环境变量的好方法
http://www-2w.blog.163.com/blog/static/97931518201021211123267/ 需要查看命令具体实现:setx machine “%path%”. 配置 ...
- 【bzoj2346】[Baltic 2011]Lamp 堆优化Dijkstra
题目描述 2255是一个傻X,他连自己家灯不亮了都不知道.某天TZ大神路过他家,发现了这一情况,于是TZ开始行侠仗义了.TZ发现是电路板的问题,他打开了电路板,发现线路根本没有连上!!于是他强大的脑力 ...
- 【bzoj1283】序列 线性规划与费用流
题目描述 给出一个长度为 的正整数序列Ci,求一个子序列,使得原序列中任意长度为 的子串中被选出的元素不超过K(K,M<=100) 个,并且选出的元素之和最大. 输入 第1行三个数N,m,k. ...
- spring分布式事务控制
应用场景问题描述解决方法多数据源配置单元测试第一种方法:最大努力一次提交模式第二种方法:最大努力一次提交模式 但使用ChainedTransactionManagerChainedTransactio ...
- 刷题总结——分糖(ssoj 容斥原理+逆元+快速幂+组合数求插板)
题目: 题目描述 有 N 个(相同的)糖果,M 个(不同的)小朋友.M 和 N 满足:1≤M≤N≤100000(105).要求:1.每个小朋友都至少有一个糖果.2.不存在正整数 X(X>=2), ...
- linux下定时任务设置
原文http://www.blogjava.net/freeman1984/archive/2010/09/23/332715.html 觉这篇文章写的挺全的,把它拿过来存在博客里,方便以后查询. 为 ...
- 你能说出SQL聚集索引和非聚集索引的区别吗?
最近突然想起前一阵和一朋友的聊天,当时他问我的问题是一个非常普通的问题:说说SQL聚集索引和非聚集索引的区别. AD:WOT2015 互联网运维与开发者大会 热销抢票 其实对于非专业的数据库操作人员来 ...