P3456 [POI2007] GRZ-Ridges and Valleys BFS-连通块思想
题目描述
Byteasar loves trekking in the hills. During the hikes he explores all the ridges and valleys in vicinity.
Therefore, in order to plan the journey and know how long it will last, he must know the number of ridgesand valleys in the area he is going to visit. And you are to help Byteasar.
Byteasar has provided you with a map of the area of his very next expedition. The map is in the shape ofa n×nn×n square. For each field (i,j)(i,j) belonging to the square(for i,j∈{1,⋯ ,n}i,j∈{1,⋯,n}), its height w(i,j)w(i,j) is given.
We say two fields are adjacent if they have a common side or a common vertex (i.e. the field (i,j)(i,j) is adjacent to the fields (i−1,j−1)(i−1,j−1),(i−1,j)(i−1,j),(i−1,j+1)(i−1,j+1),(i,j−1)(i,j−1),(i,j+1)(i,j+1),(i+1,j−1)(i+1,j−1),(i+1,j)(i+1,j),(i+1,j+1)(i+1,j+1), provided that these fields are on the map).
We say a set of fields SS forms a ridge (valley) if:
all the fields in SS have the same height,the set SS forms a connected part of the map (i.e. from any field in SS it is possible to reach any other field in SS while moving only between adjacent fields and without leaving the set SS),if s∈Ss∈S and the field s′∉Ss′∈/S is adjacent to ss, then ws>ws′ws>ws′(for a ridge) or ws<ws′ws<ws′ (for a valley).
In particular, if all the fields on the map have the same height, they form both a ridge and a valley.
Your task is to determine the number of ridges and valleys for the landscape described by the map.
TaskWrite a programme that:
reads from the standard input the description of the map, determines the number of ridges and valleys for the landscape described by this map, writes out the outcome to the standard output.
给定一张地势图,求山峰和山谷的数量
输入格式
In the first line of the standard input there is one integer nn (2≤n≤1 0002≤n≤1 000)denoting the size of the map. Ineach of the following nn lines there is the description of the successive row of the map. In (i+1)(i+1)'th line(for i∈{1,⋯ ,n}i∈{1,⋯,n}) there are nn integers w(i,1),⋯ ,w(i,n)w(i,1),⋯,w(i,n)(0≤wi≤1 000 000 0000≤wi≤1 000 000 000), separated by single spaces. Thesedenote the heights of the successive fields of the ii'th row of the map.
输出格式
The first and only line of the standard output should contain two integers separated by a single space -thenumber of ridges followed by the number of valleys for the landscape described by the map.
题意翻译
题目描述
译自 POI 2007 Stage 2. Day 0「Ridges and Valleys」
给定一个 n×n 的网格状地图,每个方格 (i,j)(i,j) 有一个高度 wij。如果两个方格有公共顶点,则它们是相邻的。
定义山峰和山谷如下:
- 均由地图上的一个连通块组成;
- 所有方格高度都相同;
- 周围的方格(即不属于山峰或山谷但与山峰或山谷相邻的格子)高度均大于山谷的高度,或小于山峰的高度。
求地图内山峰和山谷的数量。特别地,如果整个地图方格的高度均相同,则整个地图既是一个山谷,也是一个山峰。
输入格式
第一行一个整数 n (2≤n≤1000),表示地图的大小。
接下来 n 行每行 n 个整数表示地图。第 ii行有 n 个整数 wi1,wi2,…,win(0≤wij≤1 000 000 000),表示地图第 ii 行格子的高度。
输出格式
输出一行两个整数,分别表示山峰和山谷的数量。
样例解释
翻译来自于 LibreOJ。
输入输出样例
输入 #1复制
5 8 8 8 7 7 7 7 8 8 7 7 7 7 7 7 7 8 8 7 8 7 8 8 8 8
输出 #1复制
2 1
输入 #2复制
5 5 7 8 3 1 5 5 7 6 6 6 6 6 2 8 5 7 2 5 8 7 1 0 1 7
输出 #2复制
3 3
#include<iostream>
#include<cstring>
#include<algorithm>#define x first
#define y secondusing namespace std;typedef pair<int,int> PII;
const int N =1000+100;
int h[N][N];
bool cnt[N][N];
PII q[N*N];int n;/*** @brief 广度优先搜索 (BFS) 函数,用于遍历矩阵中的相邻元素。* * 该函数从给定的起始坐标 (x, y) 开始,检查其周围的 8 个相邻位置,* 判断是否存在比当前高度更高的点或更低的点,并更新 has_higher 和 has_lower 标志。* 如果存在相同高度的未访问点,则继续扩展搜索。* * @param x 起始坐标的行索引* @param y 起始坐标的列索引* @param has_higher 是否存在比当前高度更高的点* @param has_lower 是否存在比当前高度更低的点*/
void bfs(int x, int y, bool &has_higher, bool &has_lower)
{int hh = 0, tt = 0;q[0] = {x, y};cnt[x][y] = true;// BFS 遍历队列中的所有节点while (hh <= tt){PII t = q[hh++];// 遍历当前节点的 8 个相邻位置for (int i = t.x - 1; i <= t.x + 1; i++){for (int j = t.y - 1; j <= t.y + 1; j++){// 检查边界条件,防止越界if (i < 0 || j < 0 || i >= n || j >= n) continue;// 如果相邻点的高度不同,更新 has_higher 和 has_lower 标志if (h[i][j] != h[t.x][t.y]){if (h[i][j] > h[t.x][t.y]) has_higher = true;else if (h[i][j] < h[t.x][t.y]) has_lower = true;}// 如果相邻点的高度相同且未访问过,加入队列继续搜索else if (!cnt[i][j]){cnt[i][j] = true;q[++tt] = {i, j};}}}}
}int main()
{// 输入矩阵大小 nscanf("%d", &n);// 输入矩阵 h 的值for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){scanf("%d", &h[i][j]);}}int peak = 0;int val = 0;// 遍历矩阵中的每个点,使用 BFS 判断是否为山顶或山谷for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){if (!cnt[i][j]){bool has_higher = false, has_lower = false;bfs(i, j, has_higher, has_lower);// 如果没有更高的点,则为山顶if (!has_higher) peak++;// 如果没有更低的点,则为山谷if (!has_lower) val++;}}}// 输出山顶和山谷的数量cout << peak << " " << val << endl;return 0;
}
相关文章:

P3456 [POI2007] GRZ-Ridges and Valleys BFS-连通块思想
题目描述 Byteasar loves trekking in the hills. During the hikes he explores all the ridges and valleys in vicinity. Therefore, in order to plan the journey and know how long it will last, he must know the number of ridgesand valleys in the area he is goi…...
WhisperKit: Android 端测试 Whisper -- Android手机(Qualcomm GPU)部署音频大模型
WhisperKit: Android 端测试 Whisper 1.环境需要2.环境构建(1)克隆项目:(2)工具检查(make setup):(3)下载模型(make download-models)(4)Docker中构建环境(ma…...
Clickhouse(Centos)
地址信息 官网地址:Fast Open-Source OLAP DBMS - ClickHouse 下载地址:packages.clickhouse.com/rpm/stable/ 1.clickhouse-client-23.1.3.5.x86_64.rpm 2.clickhouse-common-static-23.1.3.5.x86_64.rpm 3.clickhouse-common-static-dbg-23.1.3.5.x86_…...
Yolo11改进策略:Block改进|使用FastVit的RepMixerBlock改进Yolo11,重参数重构助力Yolo11涨点(全网首发)
文章目录 摘要FastViT:一种使用结构重新参数化的快速混合视觉变换器1、简介2、相关工作3、体系结构3.1、概述3.2、FastViT3.2.1、重新参数化跳过连接3.2.2、线性训练时间过参数化3.2.3、大核卷积4、实验4.1、图像分类4.2、鲁棒性评价4.3、3D Hand网格估计4.4、语义分割和目标检…...

微信小程序-基于Vant Weapp UI 组件库的Area 省市区选择
Area 省市区选择,省市区选择组件通常与 弹出层 组件配合使用。 areaList 格式 areaList 为对象结构,包含 province_list、city_list、county_list 三个 key。 每项以地区码作为 key,省市区名字作为 value。地区码为 6 位数字,前两…...
NIO(New IO)和BIO(Blocking IO)的区别
Java中的NIO(New IO)和BIO(Blocking IO)的区别及NIO的核心组件 Java中的NIO(New IO)和BIO(Blocking IO)是两种不同的网络通信模型,各自具有独特的特性和适用场景。下面将…...

ROS1入门教程6:复杂行为处理
一、新建项目 # 创建工作空间 mkdir -p demo6/src && cd demo6# 创建功能包 catkin_create_pkg demo roscpp rosmsg actionlib_msgs message_generation tf二、创建行为 # 创建行为文件夹 mkdir action && cd action# 创建行为文件 vim Move.action# 定义行为…...

碰撞检测算法之闵可夫斯基差集法(Minkowski Difference)
在游戏开发和机器人路径规划乃至于现在比较火的自动驾驶中,我们常常需要确定两个物体是否发生碰撞,有一种通过闵可夫斯基差集法求是否相交的算法,下面将介绍一下 闵可夫斯基差集法的优势 闵可夫斯基差集法优势: 可以处理复杂的…...
【唐叔学算法】第18天:解密选择排序的双重魅力-直接选择排序与堆排序的Java实现及性能剖析
引言 在数据排序的世界里,选择排序是一类简单而直观的算法,它通过不断选取未排序部分中的最小(或最大)元素来逐步构建有序序列。今天,我们将深入探讨两种基于选择思想的排序方法——直接选择排序和堆排序,…...

2008-2020年各省技术服务水平相关指标数据
2008-2020年各省技术服务水平相关指标数据 1.时间:2008-2020年 2.指标:行政区划代码、地区、年份、信息传输、软件和信息技术服务业城镇单位就业人员(万人)、软件业务收入(万元)、高技术产品出口额占商品出口额比重(%) 3.范围&…...

机器学习DAY4续:梯度提升与 XGBoost (完)
本文将通过 XGBoost 框架来实现回归、分类和排序任务,帮助理解和掌握使用 XGBoost 解决实际问题的能力。我们将从基本的数据处理开始,逐步深入到模型训练、评估以及预测。最后,将模型进行保存和加载训练好的模型。 知识点 回归任务分类任务…...
ML-Agents:训练配置文件(一)
注:本文章为官方文档翻译,如有侵权行为请联系作者删除 Training Configuration File - Unity ML-Agents Toolkit–原文链接 常见训练器配置 关于训练,您需要做出的首要决定之一是使用哪种训练器:PPO、SAC 还是 POCA。有些训练配置…...

【物联网技术与应用】 实验13:雨滴传感器实验
实验13 雨滴传感器实验 【实验介绍】 雨滴传感器或雨滴检测传感器用于检测是否下雨以及降雨。广泛应用于汽车的雨刷系统、智能照明系统和天窗系统。 【实验组件】 ● Arduino Uno主板* 1 ● USB数据线*1 ● 雨滴传感器* 1 ● 雨滴传感器调理板* 1 ● 面包板*1 ● 9V方型…...
帝国cms电脑pc站url跳转到手机站url的方法
本文讲解一下帝国cms电脑网站跳转到手机动态网站和手机静态网站的方法,笔者以古诗词网 www.gushichi.com为例,为大家介绍操作步骤。方法一:帝国pc站跳转到手机静态站 1、假设我们有帝国cms 电脑网站www.XXX.com,手机网站m.XXX.com …...
Django models中的增删改查与MySQL SQL的对应关系
在 Django 中,models 提供了一种高层次的抽象来与数据库进行交互,使得开发者可以使用 Python 代码而非直接编写 SQL 来执行增删改查(CRUD)操作。下面将详细介绍 Django 的 ORM(对象关系映射)操作如何对应到…...

双指针——快乐数
一.题目描述 202. 快乐数 - 力扣(LeetCode) 二.题目解析 我们要判断一个数是不是快乐数要通过它的三个性质来进行判断。这个数会一直变化,由它的各个位的平方和重新构成这个数。如果这个数在变化的过程中变成了1,那么就是快乐数…...
Docker 默认安装位置迁移
一、找到 Docker 默认安装位置 [roothost-192-168-0-1 ~]# docker info Client:Version: 26.1.0Context: defaultDebug Mode: falseServer:Containers: 31Running: 31Paused: 0Stopped: 0Images: 128Server Version: 26.1.0Storage Driver: overlay2Backing Filesystem:…...

jmeter跨进程实现变量共享-全局变量
jmeter跨进程实现变量共享-全局变量 例如:登录一次,后面业务进行多线程并发场景 新增一个setUp线程组,在setUp线程组下,添加登录接口 使用json提取器,提取token Authorization $.token 0添加BeanShell 后置处理程序…...

Vue.js组件(6):echarts组件
1 前言 本章主要对常用的echars图表展示进行基本的组件封装。使用该组件前需要在项目中引入echarts。官网:Apache ECharts npm install echarts --save 2 图表组件 2.1 折线图组件 组件属性:chartId,指定图表挂载div的id,注意不…...

yolov3算法及其改进
yolov3算法及其改进 1、yolov3简介2、yolov3的改进2.1、backbone的改进2.1.1、darknet19相对于vgg16有更少的参数,同时具有更快的速度和更高的精度2.1.2、resnet101和darknet53,同样具有残差结构,精度也类似,但是darknet具有更高的速度2.2、FPN2.3、anchor-base与grid-cell…...
KubeSphere 容器平台高可用:环境搭建与可视化操作指南
Linux_k8s篇 欢迎来到Linux的世界,看笔记好好学多敲多打,每个人都是大神! 题目:KubeSphere 容器平台高可用:环境搭建与可视化操作指南 版本号: 1.0,0 作者: 老王要学习 日期: 2025.06.05 适用环境: Ubuntu22 文档说…...

HBuilderX安装(uni-app和小程序开发)
下载HBuilderX 访问官方网站:https://www.dcloud.io/hbuilderx.html 根据您的操作系统选择合适版本: Windows版(推荐下载标准版) Windows系统安装步骤 运行安装程序: 双击下载的.exe安装文件 如果出现安全提示&…...

自然语言处理——Transformer
自然语言处理——Transformer 自注意力机制多头注意力机制Transformer 虽然循环神经网络可以对具有序列特性的数据非常有效,它能挖掘数据中的时序信息以及语义信息,但是它有一个很大的缺陷——很难并行化。 我们可以考虑用CNN来替代RNN,但是…...

RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)
RabbitMQ 一、RabbitMQ概述 RabbitMQ RabbitMQ最初由LShift和CohesiveFT于2007年开发,后来由Pivotal Software Inc.(现为VMware子公司)接管。RabbitMQ 是一个开源的消息代理和队列服务器,用 Erlang 语言编写。广泛应用于各种分布…...

Rust 开发环境搭建
环境搭建 1、开发工具RustRover 或者vs code 2、Cygwin64 安装 https://cygwin.com/install.html 在工具终端执行: rustup toolchain install stable-x86_64-pc-windows-gnu rustup default stable-x86_64-pc-windows-gnu 2、Hello World fn main() { println…...
「全栈技术解析」推客小程序系统开发:从架构设计到裂变增长的完整解决方案
在移动互联网营销竞争白热化的当下,推客小程序系统凭借其裂变传播、精准营销等特性,成为企业抢占市场的利器。本文将深度解析推客小程序系统开发的核心技术与实现路径,助力开发者打造具有市场竞争力的营销工具。 一、系统核心功能架构&…...
提升移动端网页调试效率:WebDebugX 与常见工具组合实践
在日常移动端开发中,网页调试始终是一个高频但又极具挑战的环节。尤其在面对 iOS 与 Android 的混合技术栈、各种设备差异化行为时,开发者迫切需要一套高效、可靠且跨平台的调试方案。过去,我们或多或少使用过 Chrome DevTools、Remote Debug…...

uni-app学习笔记三十五--扩展组件的安装和使用
由于内置组件不能满足日常开发需要,uniapp官方也提供了众多的扩展组件供我们使用。由于不是内置组件,需要安装才能使用。 一、安装扩展插件 安装方法: 1.访问uniapp官方文档组件部分:组件使用的入门教程 | uni-app官网 点击左侧…...
数据库——redis
一、Redis 介绍 1. 概述 Redis(Remote Dictionary Server)是一个开源的、高性能的内存键值数据库系统,具有以下核心特点: 内存存储架构:数据主要存储在内存中,提供微秒级的读写响应 多数据结构支持&…...

python基础语法Ⅰ
python基础语法Ⅰ 常量和表达式变量是什么变量的语法1.定义变量使用变量 变量的类型1.整数2.浮点数(小数)3.字符串4.布尔5.其他 动态类型特征注释注释是什么注释的语法1.行注释2.文档字符串 注释的规范 常量和表达式 我们可以把python当作一个计算器,来进行一些算术…...