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

【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;	
}
http://www.xdnf.cn/news/1680.html

相关文章:

  • 中介者模式:解耦对象间复杂交互的设计模式
  • connection.cursor() 与 models.objects.filter
  • 解决编译pcl时报错‘chrono_literals‘: is not a member of ‘std‘
  • Java集成【邮箱验证找回密码】功能
  • 专家系统的基本概念解析——基于《人工智能原理与方法》的深度拓展
  • 第十节:性能优化高频题-虚拟DOM与Diff算法优化
  • 大模型工业化元年:GPT-5开启通用AI新纪元,中国技术如何破局?
  • PostgreSQL的dblink扩展模块使用方法
  • electron-updater实现自动更新
  • 【Hive入门】Hive分区与分桶深度解析:优化查询性能的关键技术
  • Windows下使用 VS Code + g++ 开发 Qt GUI 项目的完整指南
  • 深度学习小记(包括pytorch 还有一些神经网络架构)
  • 代码随想录算法训练营第二十六天
  • 4.24工作总结
  • 机器人项目管理新风口:如何高效推动智能机器人研发?
  • elasticsearch查询中的特殊字符影响分析
  • x-cmd install | brows - 终端里的 GitHub Releases 浏览器,告别繁琐下载!
  • 【MinerU】:一款将PDF转化为机器可读格式的工具——RAG加强(Docker版本)
  • Linux:git和gdb/cgdb
  • Qwen2.5简要全流程以及QA
  • 基于 CentOS 的 Docker Swarm 集群管理实战指南
  • 推理模型不需要思考,伯克利新研究推翻AI刻板印象
  • 机器学习(8)——主成分分析
  • 基于单片机的游泳馆智能管理系统
  • 【网络】TCP/IP协议学习
  • Kafka 命令行样例大全
  • 【记录手贱bug日常】IDEA 配置vmoptions后打不开,重新安装,删注册表均无用
  • 软考:数值转换知识点详解
  • 矩阵系统源码搭建账号分组功能开发全流程解析,支持OEM
  • 图论---朴素Prim(稠密图)