#2653. 「POI2007」山峰和山谷 Ridges and Valleys

news2024/10/5 14:45:32

[POI2007]GRZ-Ridges and Valleys

题面翻译

给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的。若两个格子有公共顶点,那么他们就是相邻的格子。(所以与(i,j)相邻的格子有(i-1, j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1))。我们定义一个格子的集合S为山峰(山谷)当且仅当:

1.S的所有格子都有相同的高度。

2.S的所有格子都联通3.对于s属于S,与s相邻的s’不属于S。都有ws > ws’(山峰),或者ws < ws’(山谷)。

你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。

输入 第一行包含一个正整数n,表示地图的大小(1<=n<=1000)。接下来一个n*n的矩阵,表示地图上每个格子的高度。(0<=w<=1000000000)

输出 应包含两个数,分别表示山峰和山谷的数量。

感谢@Blizzard 提供的翻译

题目描述

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

大致思路

根据题意,我们需要进行两次的判断,一次是山峰,一次是山谷。
本蒟蒻的思路是尝试将整个图push入队,用数组判断是否入队过,每个数进行周围8个位置的判断,山峰如果有大于当前数的,那么这个不是山峰,但我们依然将其入队完成连通块的搜索,打好标记,避免重复,但不计入答案。如果有与当前数相等的,那将其入队,进行下一次的判断。代码实现:

memset(fup,-1,sizeof(fup));
int dx[]={0,-1,0,1,1,1,0,-1,-1};
int dy[]={0,1,1,1,0,-1,-1,-1,0};
int checkup(int xx,int yy){
	for(int i=1;i<=8;i++){
	if(xx+dx[i]<=0||xx+dx[i]>n||yy+dy[i]<=0||yy+dy[i]>n)continue;
		if(fup[xx+dx[i]][yy+dy[i]]==-1){
			if(m[xx+dx[i]][yy+dy[i]]==m[xx][yy]){
				fup[xx+dx[i]][yy+dy[i]]=1;
				q.push(node(xx+dx[i],yy+dy[i]));
			}
		}
		 if(m[xx+dx[i]][yy+dy[i]]>m[xx][yy]){
			return 1;
		}
	}
	return 99;
}
or(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(fup[i][j]==-1){
				flag=0;
				fup[i][j]=1;
				q.push(node(i,j));
				while(!q.empty()){
					k=q.front();
					q.pop();
					if(k.x<=0||k.x>n||k.y<=0||k.y>n)continue;
					if(checkup(k.x,k.y)==1)
						flag=1;
				}
				if(flag==0){
					ansup++;
				}
			}
		}
	}

记得标记数组清零,队列清零,位置边界!!!!!!!!!!
山谷的判断与山峰的判断基本一致,代码与其说像,不如说基本一致。只不过判断变为了如果周围有小于当前位置数不计入答案,标记数组初值变为最大值。代码实现:

int dx[]={0,-1,0,1,1,1,0,-1,-1};
int dy[]={0,1,1,1,0,-1,-1,-1,0};
memset(fdw,0x7f7f7f7f,sizeof(fdw));
int checkdw(int xx,int yy){
	for(int i=1;i<=8;i++){
		if(xx+dx[i]<=0||xx+dx[i]>n||yy+dy[i]<=0||yy+dy[i]>n)continue;
		if(fdw[xx+dx[i]][yy+dy[i]]==0x7f7f7f7f){
			if(m[xx+dx[i]][yy+dy[i]]==m[xx][yy]){
				fdw[xx+dx[i]][yy+dy[i]]=1;
				q.push(node(xx+dx[i],yy+dy[i]));
			}
		}
		if(m[xx+dx[i]][yy+dy[i]]<m[xx][yy]){
			return 1;
		}
	}
	return 99;
}
for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(fdw[i][j]==0x7f7f7f7f){
				flag=0;
				fdw[i][j]=1;
				q.push(node(i,j));
				while(!q.empty()){					
					k=q.front();
					q.pop();
					if(k.x<=0||k.x>n||k.y<=0||k.y>n)continue;
					if(checkdw(k.x,k.y)==1)
						flag=1;
				}
				if(flag==0){
					ansdw++;
				}
			}
		}
	}

其实这个题像是两个题拼起来的

AC CODE

#include<bits/stdc++.h>
using namespace std;
int fup[1222][1222],fdw[1222][1222];
int n,m[1999][1999];
struct node{
	int x,y;
	node(int xx=0,int yy=0)/*:x(x),y(y)*/{
		this->x=xx;
		this->y=yy;
	}
}k;
int ansup=0,ansdw=0;
bool flag=0;
queue<node>q;
int dx[]={0,-1,0,1,1,1,0,-1,-1};
int dy[]={0,1,1,1,0,-1,-1,-1,0};
int checkup(int xx,int yy){
	for(int i=1;i<=8;i++){
	if(xx+dx[i]<=0||xx+dx[i]>n||yy+dy[i]<=0||yy+dy[i]>n)continue;
		if(fup[xx+dx[i]][yy+dy[i]]==-1){
			if(m[xx+dx[i]][yy+dy[i]]==m[xx][yy]){
				fup[xx+dx[i]][yy+dy[i]]=1;
				q.push(node(xx+dx[i],yy+dy[i]));
			}
		}
		 if(m[xx+dx[i]][yy+dy[i]]>m[xx][yy]){
			return 1;
		}
	}
	return 99;
}
int checkdw(int xx,int yy){
	for(int i=1;i<=8;i++){
		if(xx+dx[i]<=0||xx+dx[i]>n||yy+dy[i]<=0||yy+dy[i]>n)continue;
		if(fdw[xx+dx[i]][yy+dy[i]]==0x7f7f7f7f){
			if(m[xx+dx[i]][yy+dy[i]]==m[xx][yy]){
				fdw[xx+dx[i]][yy+dy[i]]=1;
				q.push(node(xx+dx[i],yy+dy[i]));
			}
		}
		if(m[xx+dx[i]][yy+dy[i]]<m[xx][yy]){
			return 1;
		}
	}
	return 99;
}
int main(){
	cin>>n;
	memset(fup,-1,sizeof(fup));
	memset(fdw,0x7f7f7f7f,sizeof(fdw));
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>m[i][j];
		}
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(fup[i][j]==-1){
				flag=0;
				fup[i][j]=1;
				q.push(node(i,j));
				while(!q.empty()){
					k=q.front();
					q.pop();
					if(k.x<=0||k.x>n||k.y<=0||k.y>n)continue;
					if(checkup(k.x,k.y)==1)
						flag=1;
				}
				if(flag==0){
					ansup++;
				}
			}
		}
	}
	while(!q.empty())q.pop();
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(fdw[i][j]==0x7f7f7f7f){
				flag=0;
				fdw[i][j]=1;
				q.push(node(i,j));
				while(!q.empty()){					
					k=q.front();
					q.pop();
					if(k.x<=0||k.x>n||k.y<=0||k.y>n)continue;
					if(checkdw(k.x,k.y)==1)
						flag=1;
				}
				if(flag==0){
					ansdw++;
				}
			}
		}
	}
	cout<<ansup<<" "<<ansdw<<endl;
	return 0;
} 

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

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

相关文章

IntelliJ IDEA 2022.3.1 (Community Edition)代码注释风格设置

Setting -> Editor -> Code Style -> Java -> Comment Code

Unity 之 最新Ads原生广告接入流程详解和工具类分享

Unity 之 Ads接入流程详解 一&#xff0c;注册 Unity Ads 广告 SDK二&#xff0c;下载 Unity Ads 广告 SDK三&#xff0c;配置 Unity Ads 广告 SDK3.1 广告位展示流程3.2 代码初始化 四&#xff0c;集成 Unity Ads 广告 SDK4.1 相关介绍4.2 代码分享 五&#xff0c;测试 Unity …

C语言变量学习2

前文已经学习了C语言变量&#xff1b; C语言变量_c语言变量块_bcbobo21cn的博客-CSDN博客 继续再学习&#xff1b;VC6新建一个单文档工程&#xff1b; void CVtestView::OnDraw(CDC* pDC) {CVtestDoc* pDoc GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for na…

【C语言复习】第三篇、Gitee码云的创建和使用

目录 第一部分、Gitee码云的用处 1、为什么要使用Gitee&#xff1f; 2、我参考的视频 第二部分、软件安装流程 1、下载Git for windows软件和TortoiseGit软件 2、Git for windows软件的安装流程 3、TortoiseGit软件软件的安装流程 第三部分、Gitee如何创建仓库&#xf…

jquery和jquery ui有什么区别

jquery和jquery ui有什么区别 jquery和jquery ui有什么区别 jQuery UI 与 jquery 的主要区别是&#xff1a;(1) jQuery是一个js库&#xff0c;主要提供的功能是选择器&#xff0c;属性修改和事件绑定等等。(2) jQuery UI则是在jQuery的基础上&#xff0c;利用jQuery的扩展性&…

企业搭建小型直播间、中型直播间、大型直播间的介绍

常用的直播间搭建大致分为实景和绿幕两种类型。实景区即公司具体场景&#xff0c;如会客厅、LOGO墙、海报展板以及纯白背景等&#xff0c;根据企业直播主题而调整&#xff1b;绿幕区则是通过搭建绿幕&#xff0c;满足企业直播过程中需要进行抠像&#xff0c;设置沉浸式背景的需…

国产麒麟服务器等保二级 配置规范(一)

麒麟linux的加固 1.检查设备密码复杂度策略 配置文件 vi /etc/pam.d/system-auth password requisite pam_cracklib.so ucredit-1 lcredit-1 dcredit-1 minlen6 auth required pam_tally.so deny5 unlock_time600 no_lock_time account required pam_tally.sopassword suffi…

大数据Doris(四十五):Routine Load注意事项

文章目录 Routine Load注意事项 Routine Load注意事项 1、查看作业状态的具体命令和示例可以通过 HELP SHOW ROUTINE LOAD; 命令查看。 2、用户可以通过 STOP/PAUSE/RESUME 三个命令来控制作业的停止,暂停和重启。可以通过 HELP STOP ROUTINE LOAD; HELP PAUSE ROUTINE LOA…

调用万维易源API实现证件照换装

目录 1. 作者介绍2. 万维易源API简介2.1 易源数据2.2 易源API管理2.3 调用参数简介 3. 万维易源API调用过程3.1 获取ID和代码3.2 代码实现3.3 完整代码 参考&#xff08;可供参考的链接和引用文献&#xff09; 1. 作者介绍 吴宇&#xff0c;男&#xff0c;西安工程大学电子信息…

LVS keepalived 集群

LVS keepalived 集群 LVS keepalived 集群一、Keepalived及其工作原理1.Keepalived体系主要模块及其作用2.健康检查的方式&#xff08;探针&#xff09; 二、LVSKeepalived 高可用群集部署1.LVS部署一&#xff1a;配置负载调度器&#xff08;主、备相同&#xff09;二&#xff…

FreeRTOS实时操作系统(二)任务创建与任务删除(HAL库)

文章目录 前言系统配置任务创建动态任务创建删除实践静态任务创建删除实践 前言 接着学习正点原子的FreeRTOS教程&#xff0c;涉及到一些详细的系统内文件代码 系统配置 可以通过各种的宏定义来实现我们自己的RTOS配置&#xff08;在FreeRTOSconfig.h&#xff09; “INCLUDE…

100天精通Python(可视化篇)——第92天:Pyecharts绘制炫酷柱状图、条形图实战大全

文章目录 专栏导读1. 基础柱状图2. 旋转x轴标签3. 旋转坐标轴4. 添加坐标轴名称5. 添加标记点6. 添加标注线7. 添加数据8. 添加自定义背景图9. 堆叠柱状图10. 柱状图与折线图组合11. 三维柱状图12. 水平滑动、鼠标滚轮缩放柱状图 专栏导读 &#x1f525;&#x1f525;本文已收…

ConcurrentHashMap源码

HashTable是一个线程安全的类&#xff0c;它使用synchronized来锁住整张Hash表来实现线程安全&#xff0c;即每次锁住整张表让线程独占&#xff0c;相当于所有线程进行读写时都去竞争一把锁&#xff0c;导致效率非常低下。 介绍 ConcurrentHashMap的底层原理和HashMap是比较相…

网页之http

目录 一、网页概念&#xff1a; 1.纯文本格式文件 2.编写语言-----html---超文本标记语言 3.浏览器相当于翻译器&#xff0c;检查是否为html文件&#xff0c;是的话就翻译&#xff0c;否则就报错。 二、域名 三、DNS解析 1.分布式域名解析-----层次性&#xff1a;迭代处…

谁是远程界的天花板?2023年5款最常用的远程软件横测:ToDesk、向日葵、TeamViewer、Splashtop、AnyDesk

前言 一个优秀的远控软件&#xff0c;追求的是可信赖的安全感&#xff0c;连接的流畅度、画质的清晰度、操作的简单化、毫秒级的无感延迟以及全方位的功能。另外&#xff0c;远控软件还应拥有独立的创新技术&#xff0c;具备竞争对手无法超越的市场前瞻性&#xff0c;与世界保…

MySQL - 第4节 - MySQL表的约束

1.MySQL表的约束概述 • 真正约束字段的是数据类型&#xff0c;如果插入的数据超出了对应数据类型的取值范围&#xff0c;那么数据将会插入失败。 • 但是数据类型的约束很单一&#xff0c;为了更好的保证数据的合法性&#xff0c;从业务逻辑角度保证数据的正确性&#xff0c;M…

ldr、str、ldm、stm、msr、mrs、swi、svc、mrc等ARM指令详解及具体应用

文章目录 前言一、跳转指令1.1 相对跳转1.2 绝对跳转 二、内存操作指令2.1 Load和Store2.1.1 伪指令2.1.2 伪操作 2.2 内存操作指令具体应用 三、 寄存器的寻址方式3.1 前索引寻址3.2 后索引寻址3.3 基址变址 四、块拷贝指令&#xff08;多数据加载&#xff09;4.1 块拷贝4.2 指…

Nacos 源码分析全系列

Nacos 源码分析全系列 学习目标 主线任务 代码解析画图git库(中文注释)设计思想多版本迭代讨论群(私聊进群) 主要的大纲路线 主要拆解的是nacos的1.4.1版本和2.1.0版本,还有nacos 的一些已知的bug 正确的学习源码的姿势 服务端是如何处理客户度的请求 注册中心服务 内存…

【备战秋招】每日一题:华东师范大学保研机试-2022-差分计数

为了更好的阅读体检&#xff0c;可以查看我的算法学习博客差分计数 题目内容 给定n个整数,...,和一个整数x。求有多少有序对(i,j)满足 输入格式 第一行两个整数,分别代表整数的个数和题目中的x。 第二行n个用空格隔开的整数&#xff0c;第i个代表 输出格式 一行一个整数…

1745_Perl中的switch结构

全部学习汇总&#xff1a; GreyZhang/perl_basic: some perl basic learning notes. (github.com) 用了很久时间的Perl了&#xff0c;但是一直没有使用过switch结构。即使有的时候&#xff0c;基本上也通过其他的形式完成了相关工作。虽说有时候可能会效率低一些&#xff0c;但…