洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm
https://www.luogu.org/problem/show?pid=2919
题目描述
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
每次从最高点向四周扩展、
#include <algorithm>
#include <cstdio> #define max(a,b) (a>b?a:b)
inline void read(int &x)
{
x=; register char ch=getchar();
for(; ch>''||ch<''; ) ch=getchar();
for(; ch>=''&&ch<=''; ch=getchar()) x=x*+ch-'';
}
const int N();
int fx[]={-,,,-,,-,,};
int fy[]={-,-,-,,,,,};
int n,m,H,ans,cnt,h[N][N];
struct Pos {
int x,y,h;
bool operator < (const Pos a)const
{
return h>a.h;
}
}pos[N*N];
bool vis[N][N];
void DFS(int x,int y)
{
vis[x][y]=;
for(int tx,ty,i=; i<; ++i)
{
tx=x+fx[i]; ty=y+fy[i];
if(tx<||ty<||tx>n||ty>m) continue;
if(!vis[tx][ty]&&h[x][y]>=h[tx][ty]) DFS(tx,ty);
}
} int Presist()
{
read(n),read(m);
for(int i=; i<=n; ++i)
for(int j=; j<=m; ++j)
read(pos[++cnt].h),pos[cnt].x=i,pos[cnt].y=j,h[i][j]=pos[cnt].h;
std::sort(pos+,pos+cnt+);
for(int i=; i<=cnt; ++i)
if(!vis[pos[i].x][pos[i].y]) DFS(pos[i].x,pos[i].y),ans++;
printf("%d\n",ans);
return ;
} int Aptal=Presist();
int main(int argc,char*argv[]){;}
洛谷—— P2919 [USACO08NOV]守护农场Guarding the Farm的更多相关文章
- 洛谷——P2919 [USACO08NOV]守护农场Guarding the Farm
P2919 [USACO08NOV]守护农场Guarding the Farm 题目描述 The farm has many hills upon which Farmer John would li ...
- 洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
题目描述 The farm has many hills upon which Farmer John would like to place guards to ensure the safety ...
- 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\)的这个序列的排列有多少个? 范围: ...
随机推荐
- Java多线程(四)isAlive
isAlive 活动状态:线程处于正在运行或准备开始运行的状态 public class ISLiveDemo extends Thread { public void run() { System. ...
- Docker (1) 基本概念和安装
Docker简介 什么是容器? 一种虚拟化的方案,操作系统级别的虚拟化.容器是一个轻量的.独立的.可执行的包,包含了执行它所需要的所有东西:代码.运行环境.系统工具.系统库.设置.很长一段时间中,容器 ...
- Log4Net学习笔记(1)-完整的例子
一.开发环境 编译器:VS2013 .Net版本:4.5 二.开发流程 1.从nuget上获取log4net 2.配置log4net的配置文件 <?xml version="1.0&q ...
- 我要上google
我要上google 一.下载google浏览器(百度下载) 二.获取和运行xx-net 1.https://github.com/XX-net/XX-Net 2.解压下载的xx-net,运行文件夹中的 ...
- [ USACO 2001 OPEN ] 地震
\(\\\) Description 给出一张 \(n\) 个点 \(m\) 条边的无向图,现在要建一棵生成树. 每条边都有消耗的时间 \(t_i\),也有建造的代价 \(w_i\) . 最后总金给 ...
- JavaScript(七)数组
Array类型 1.创建数组 字面量 var arr = [];//不要在低版本的浏览其中创建字面量的时候最后 //一个item后面加 逗号 低版本会 再新建一个空的item 构造函数 var arr ...
- (8)string对象上的操作1
读写操作 //读写string对象的测试.//本程序输入两string类,输出两string类. #include <iostream> #include <string> ...
- 微信小程序组件解读和分析:五、text文本
text文本组件说明: text 文本就是微信小程序中显示出来的文本. text文本组件的示例代码运行效果如下: 下面是WXML代码: [XML] 纯文本查看 复制代码 ? 1 2 3 4 <v ...
- Java反射机制实战——字段篇
首先,我们来认识几个类. Class(java.lang.Class) Class对象是一个特殊对象,每一个类都有一个Class对象,用来创建该类的“常规”对象.可以通过对象的getClass()方法 ...
- R语言曲线拟合函数(绘图)
曲线拟合:(线性回归方法:lm) 1.x排序 2.求线性回归方程并赋予一个新变量 z=lm(y~x+I(x^2)+...) 3.plot(x,y) #做y对x的散点图 4.lines(x ...