当前位置: 首页 > news >正文

【C++ 真题】P3456 [POI2007] GRZ-Ridges and Valleys

[POI2007] GRZ-Ridges and Valleys

题面翻译

题目描述

译自 POI 2007 Stage 2. Day 0「Ridges and Valleys」

给定一个 n × n n \times n n×n 的网格状地图,每个方格 ( i , j ) (i,j) (i,j) 有一个高度 w i j w_{ij} wij。如果两个方格有公共顶点,则它们是相邻的。

定义山峰和山谷如下:

  • 均由地图上的一个连通块组成;
  • 所有方格高度都相同;
  • 周围的方格(即不属于山峰或山谷但与山峰或山谷相邻的格子)高度均大于山谷的高度,或小于山峰的高度。

求地图内山峰和山谷的数量。特别地,如果整个地图方格的高度均相同,则整个地图既是一个山谷,也是一个山峰。

输入格式

第一行一个整数 n n n 2 ≤ n ≤ 1000 2 \le n \le 1000 2n1000),表示地图的大小。

接下来 n n n 行每行 n n n 个整数表示地图。第 i i i 行有 n n n 个整数 w i 1 , w i 2 , … , w i n ( 0 ≤ w i j ≤ 1 000 000 000 ) w_{i1}, w_{i2}, \ldots, w_{in} (0 \le w_{ij} \le 1\ 000\ 000\ 000) wi1,wi2,,win(0wij1 000 000 000),表示地图第 i i i 行格子的高度。

输出格式

输出一行两个整数,分别表示山峰和山谷的数量。

样例解释

翻译来自于 LibreOJ。

题目描述

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 × n n\times n n×n square. For each field ( i , j ) (i,j) (i,j) belonging to the square(for i , j ∈ { 1 , ⋯ , n } i,j\in \{1,\cdots,n\} i,j{1,,n}), its height w ( i , j ) 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) (i,j) is adjacent to the fields ( i − 1 , j − 1 ) (i-1,j-1) (i1,j1), ( i − 1 , j ) (i-1,j) (i1,j), ( i − 1 , j + 1 ) (i-1,j+1) (i1,j+1), ( i , j − 1 ) (i,j-1) (i,j1), ( i , j + 1 ) (i,j+1) (i,j+1), ( i + 1 , j − 1 ) (i+1,j-1) (i+1,j1), ( i + 1 , j ) (i+1,j) (i+1,j), ( i + 1 , j + 1 ) (i+1,j+1) (i+1,j+1), provided that these fields are on the map).

We say a set of fields S S S forms a ridge (valley) if:

all the fields in S S S have the same height,the set S S S forms a connected part of the map (i.e. from any field in S S S it is possible to reach any other field in S S S while moving only between adjacent fields and without leaving the set S S S),if s ∈ S s\in S sS and the field s ′ ∉ S s'\notin S s/S is adjacent to s s s, then w s > w s ′ w_s>w_{s'} ws>ws(for a ridge) or w s < w s ′ w_s<w_{s'} 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 n n n ( 2 ≤ n ≤ 1 000 2\le n\le 1\ 000 2n1 000)denoting the size of the map. Ineach of the following n n n lines there is the description of the successive row of the map. In ( i + 1 ) (i+1) (i+1)'th line(for i ∈ { 1 , ⋯ , n } i\in \{1,\cdots,n\} i{1,,n}) there are n n n integers w ( i , 1 ) , ⋯ , w ( i , n ) w_{(i,1)},\cdots,w_{(i,n)} w(i,1),,w(i,n)( 0 ≤ w i ≤ 1 000 000 000 0\le w_i\le 1\ 000\ 000\ 000 0wi1 000 000 000), separated by single spaces. Thesedenote the heights of the successive fields of the i i i’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.

样例 #1

样例输入 #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

样例输入 #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<bits/stdc++.h>
using namespace std;
const int N = 1e3+7;long long f[N][N];
int n, sf = 0, sg = 0;
int dx[] = {0, -1, -1, 0, 1, 1, 1, 0, -1}, dy[] = {0, 0, 1, 1, 1, 0, -1, -1, -1};	//上右下左顺时针旋转 
bool vis[N][N], isf = false, isg = false; void dfs(int x,int y){vis[x][y] = 1; for(int i=1;i<=8;++i){int nx = x + dx[i], ny = y + dy[i];	 if(nx < 0 || nx >= n || ny < 0 || ny >= n) continue;if(f[nx][ny] == f[x][y]){if(vis[nx][ny]) continue;dfs(nx, ny);}else if(f[nx][ny] > f[x][y]) isg = true;//看来是山谷else if(f[nx][ny] < f[x][y]) isf = true;//山峰}//不用 vis[x][y] = 0;return ;
}int main(){cin>>n;for(int i=0;i<n;++i){for(int j=0;j<n;++j){cin>>f[i][j];}}for(int i=0;i<n;++i){for(int j=0;j<n;++j){isf = false;//初始化isg = false;if(!vis[i][j]){//没走过dfs(i, j);if(isf && !isg)	sf++;//只是山峰不是山谷else if(!isf && isg) sg++;//不是山峰只是山谷}}} if(sf == 0 && sg == 0) cout<<"1 1"<<endl;//特判 else cout<<sf<<" "<<sg<<endl;return 0;	
}

相关文章:

  • AI大模型从0到1记录学习 数据结构和算法 day20
  • 【Linux】网络基础和socket(4)
  • SQL进阶知识:六、动态SQL
  • 济南国网数字化培训班学习笔记-第二组-5节-输电线路设计
  • 解决 PostgreSQL 检查约束导致的数据插入异常问题
  • 网络IP冲突的成因与解决方案
  • 三维重建模块VR,3DCursor,MPR与VR的坐标转换
  • 二叉树的创建,增加,前序遍历
  • Bytebase 取得 SOC 2 Type 1 认证
  • 第55讲:农业人工智能的跨学科融合与社会影响——构建更加可持续、包容的农业社会
  • YOLOv5改进(十)-- 轻量化模型MobileNetv4
  • Sharding-JDBC 系列专题 - 第十篇:ShardingSphere 生态与未来趋势
  • PHYBench:首个大规模物理场景下的复杂推理能力评估基准
  • C++23文本编码革新:迈向更现代的字符处理
  • 2025年3月电子学会青少年机器人技术(五级)等级考试试卷-理论综合
  • 10.接口而非实现编程
  • CentOS 7上Memcached的安装、配置及高可用架构搭建
  • LLM学习笔记4——本地部署Docker、vLLM和Qwen2.5-32B-Instruct实现OpenManus的使用
  • Qt 中线程使用
  • shell练习题(1)
  • 期待会师!神二十与空间站完成对接
  • 贝壳:网传“深圳贝壳内部通知”不实
  • 梁启超“失肾记”的余波:中西医论战与最后的真相
  • GDP十强省份“一季报”出炉,湖北领跑
  • 金湘军辞去山西省省长职务
  • 灰鹦鹉爆粗口三年未改?云南野生动物园:在持续引导