洛谷 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

 

输入输出样例

输入样例#1:
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 
输出样例#1:
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.

 

 

搜索

屠龙宝刀点击就送

#include <algorithm>
#include <cstdio>
#include <queue>
#define N 705using namespace std;
struct node
{int h,x,y;bool operator<(node a)const{return h>a.h;}
}sf[N*N];
bool vis[N][N];
int n,m,ans,tot,G[N][N],fx[8]={1,-1,0,0,1,1,-1,-1},fy[8]={0,0,-1,1,1,-1,-1,1};
struct Node
{int x,y;
};
void bfs(int x,int y)
{queue<Node>Q;Q.push((Node){x,y});vis[x][y]=1;for(Node now;!Q.empty();){now=Q.front();Q.pop();int H=G[now.x][now.y];for(int i=0;i<8;++i){int tx=now.x+fx[i],ty=now.y+fy[i];if(tx<1||tx>n||ty<1||ty>m||vis[tx][ty]||G[tx][ty]>H) continue;Q.push((Node){tx,ty});vis[tx][ty]=1;}}
}
int Main()
{scanf("%d%d",&n,&m);for(int i=1;i<=n;++i){for(int j=1;j<=m;++j){scanf("%d",&sf[++tot].h);sf[tot].x=i;sf[tot].y=j;G[i][j]=sf[tot].h;}}sort(sf+1,sf+1+tot); for(int i=1;i<=tot;++i){if(vis[sf[i].x][sf[i].y])continue;bfs(sf[i].x,sf[i].y);ans++;}printf("%d\n",ans);return 0;
}
int sb=Main();
int main(int argc,char *argv[]){;}

 

转载于:https://www.cnblogs.com/ruojisun/p/7607929.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/391263.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

iOS 开发一定要尝试的 Texture(ASDK)

原文链接 - iOS 开发一定要尝试的 Texture(ASDK)(排版正常, 包含视频) 前言 本篇所涉及的性能问题我都将根据滑动的流畅性来评判, 包括掉帧情况和一些实际体验 ASDK 已经改名为 Texture, 我习惯称作 ASDK 编译环境: MacOS 10.13.3, Xcode 9.2 参与测试机型: iPhone 6 10.3.3, i…

lisp语言是最好的语言_Lisp可能不是数据科学的最佳语言,但是我们仍然可以从中学到什么呢?...

lisp语言是最好的语言This article is in response to Emmet Boudreau’s article ‘Should We be Using Lisp for Data-Science’.本文是对 Emmet Boudreau的文章“我们应该将Lisp用于数据科学”的 回应 。 Below, unless otherwise stated, lisp refers to Common Lisp; in …

链接访问后刷新颜色回到初始_如何使链接可访问(提示:颜色不够)

链接访问后刷新颜色回到初始Link accessibility is one of the most important aspects of usability. However, designers often dont understand what it takes to make links accessible. Most frequently, they only distinguish links by color, which makes it hard for …

567

567 转载于:https://www.cnblogs.com/Forever77/p/11519678.html

leetcode 403. 青蛙过河(dp)

一只青蛙想要过河。 假定河流被等分为若干个单元格&#xff0c;并且在每一个单元格内都有可能放有一块石子&#xff08;也有可能没有&#xff09;。 青蛙可以跳上石子&#xff0c;但是不可以跳入水中。 给你石子的位置列表 stones&#xff08;用单元格序号 升序 表示&#xff…

static、volatile、synchronize

原子性&#xff08;排他性&#xff09;&#xff1a;不论是多核还是单核&#xff0c;具有原子性的量&#xff0c;同一时刻只能有一个线程来对它进行操作&#xff01;可见性&#xff1a;多个线程对同一份数据操作&#xff0c;thread1改变了某个变量的值&#xff0c;要保证thread2…

tensorflow基本教程

转载自 http://tensornews.cn/ 转载于:https://www.cnblogs.com/Chris-01/p/11523316.html

1.10-linux三剑客之sed命令详解及用法

内容:1.sed命令介绍2.语法格式,常用功能查询 增加 替换 批量修改文件名第1章 sed是什么字符流编辑器 Stream Editor第2章 sed功能与版本处理出文本文件,日志,配置文件等增加,删除,修改,查询sed --versionsed -i 修改文件内容第3章 语法格式3.1 语法格式sed [选项] [sed指令…

python pca主成分_超越“经典” PCA:功能主成分分析(FPCA)应用于使用Python的时间序列...

python pca主成分FPCA is traditionally implemented with R but the “FDASRSF” package from J. Derek Tucker will achieve similar (and even greater) results in Python.FPCA传统上是使用R实现的&#xff0c;但是J. Derek Tucker的“ FDASRSF ”软件包将在Python中获得相…

blender视图缩放_如何使用主视图类型缩放Elm视图

blender视图缩放A concept to help Elm Views scale as applications grow larger and more complicated.当应用程序变得更大和更复杂时&#xff0c;可帮助Elm Views扩展的概念。 In Elm, there are a lot of great ways to scale the Model, and update, but there is more c…

初探Golang(2)-常量和命名规范

1 命名规范 1.1 Go是一门区分大小写的语言。 命名规则涉及变量、常量、全局函数、结构、接口、方法等的命名。 Go语言从语法层面进行了以下限定&#xff1a;任何需要对外暴露的名字必须以大写字母开头&#xff0c;不需要对外暴露的则应该以小写字母开头。 当命名&#xff08…

789

789 转载于:https://www.cnblogs.com/Forever77/p/11524161.html

sql的split()函数

ALTER function [dbo].[StrToList_Test](Str varchar(max), fg NVARCHAR(200)) returns table table(value nvarchar(max) ) as begindeclare tempStr nvarchar(max),len INT LEN(fg); --去除前后分割符 while substring(Str,1,len)fg beginset Strsubstring(Str,len1,len(S…

大数据平台构建_如何像产品一样构建数据平台

大数据平台构建重点 (Top highlight)Over the past few years, many companies have embraced data platforms as an effective way to aggregate, handle, and utilize data at scale. Despite the data platform’s rising popularity, however, little literature exists on…

初探Golang(3)-数据类型

Go语言拥有两大数据类型&#xff0c;基本数据类型和复合数据类型。 1. 数值类型 ##有符号整数 int8&#xff08;-128 -> 127&#xff09; int16&#xff08;-32768 -> 32767&#xff09; int32&#xff08;-2,147,483,648 -> 2,147,483,647&#xff09; int64&#x…

freecodecamp_freeCodeCamp的服务器到底发生了什么?

freecodecampUpdate at 17:00 California time: We have now fixed most of the problems. Were still working on a few known issues, but /learn is now fully operational.加利福尼亚时间17:00更新 &#xff1a;我们现在解决了大多数问题。 我们仍在处理一些已知问题&#…

为什么Linux下的环境变量要用大写而不是小写

境变量的名称通常用大写字母来定义。实际上用小写字母来定义环境变量也不会报错&#xff0c;只是习惯上都是用大写字母来表示的。 首先说明一下&#xff0c;在Windows下是不区分大小写的&#xff0c;所以在Windows下怎么写都能获取到值。 而Linux下不同&#xff0c;区分大小写&…

python:连接Oracle数据库后控制台打印中文为??

打印查询结果&#xff0c;中文显示为了&#xff1f;&#xff1f;&#xff1f; [(72H FCR, 2.0), (?????, 8.0)] E:\Python35\Lib\site-packages中新增文件&#xff1a; sitecustomize.py import os os.environ[NLS_LANG] SIMPLIFIED CHINESE_CHINA.UTF8 转载于:https://w…

时间序列预测 时间因果建模_时间序列建模以预测投资基金的回报

时间序列预测 时间因果建模Time series analysis, discussed ARIMA, auto ARIMA, auto correlation (ACF), partial auto correlation (PACF), stationarity and differencing.时间序列分析&#xff0c;讨论了ARIMA&#xff0c;自动ARIMA&#xff0c;自动相关(ACF)&#xff0c;…

初探Golang(4)-map和流程控制语句

1.map map 是引用类型的&#xff0c;如果声明没有初始化值&#xff0c;默认是nil。空的切片是可以直接使用的&#xff0c;因为他有对应的底层数组,空的map不能直接使用。需要先make之后才能使用。 //1, 声明map 默认值是nil var m1 map[key_data_type]value_data_type 声明 …