华为OD-D卷多段线数据压缩

embedded/2024/9/25 1:13:33/

下图中,每个方块代表一个像素,每个像素用其行号和列号表示。

为简化处理,多段线的走向只能是水平、竖直、斜向45度。

上图中的多段线可以用下面的坐标串表示:(2, 8), (3, 7), (3, 6), (3, 5), (4, 4), (5, 3), (6, 2), (7, 3), (8, 4), (7, 5)。

但可以发现,这种表示不是最简的,其实只需要存储6个蓝色的关键点即可,它们是线段的起点、拐点、终点,而剩下4个点是冗余的。

现在,请根据输入的包含有冗余数据的多段线坐标列表,输出其最简化的结果。

输入描述:

2 8 3 7 3 6 3 5 4 4 5 3 6 2 7 3 8 4 7 5

1、所有数字以空格分隔,每两个数字一组,第一个数字是行号,第二个数字是列号;

2、行号和列号范围为[0,64),用例输入保证不会越界,考生不必检查;

3、输入数据至少包含两个坐标点。

输出描述:

2 8 3 7 3 5 6 2 8 4 7 5

压缩后的最简化坐标列表,和输入数据的格式相同。

备注:

输出的坐标相对顺序不能变化。

 题目解析:看似有点复杂,实际上每一步只有八种变化方式,上下左右,以及四个对角,可以通过两个变量分别记录当前变化的方式和上一步变化的方式,两次变化方式一致则可以缩减,剔除的是上一步的数据,但是取出的时候需要保持顺序不变,也就是前后都有操作,选择双端队列操作即可!

import java.util.*;public class Main {public static void main(String[] args) {
//        int[] nums = new int[]{2, 8, 3, 7, 3, 6, 3, 5, 4, 4, 5, 3, 6, 2, 7, 3, 8, 4, 7, 5};Scanner scanner = new Scanner(System.in);String[] s = scanner.nextLine().split(" ");int[] nums = new int[s.length];for (int i = 0; i < nums.length; i++) {nums[i] = Integer.parseInt(s[i]);}Deque<Integer> x = new LinkedList<>();Deque<Integer> y = new LinkedList<>();// 题目中提到输入至少有两个数x.push(nums[0]);y.push(nums[1]);int type = 0;int frontType = 0;for (int i = 2; i < nums.length; i += 2) {int x1 = nums[i];int y1 = nums[i + 1];// 判断类型(可以再优化下这里)if (x1 - x.peekLast() == -1 && y1 - y.peekLast() == 0) {type = 1;} else if (x1 - x.peekLast() == 1 && y1 - y.peekLast() == 0) {type = 2;} else if (x1 - x.peekLast() == 0 && y1 - y.peekLast() == -1) {type = 3;} else if (x1 - x.peekLast() == -1 && y1 - y.peekLast() == -1) {type = 4;} else if (x1 - x.peekLast() == 1 && y1 - y.peekLast() == -1) {type = 5;} else if (x1 - x.peekLast() == 0 && y1 - y.peekLast() == 1) {type = 6;} else if (x1 - x.peekLast() == -1 && y1 - y.peekLast() == 1) {type = 7;} else if (x1 - x.peekLast() == 1 && y1 - y.peekLast() == 1) {type = 8;}if (frontType == 0 || frontType != type) {frontType = type;// 删除上一步位置} else if (frontType == type) {x.pollLast();y.pollLast();}// 放入当前位置x.add(nums[i]);y.add(nums[i + 1]);}// 注意输出最后没有空格while (x.size() > 1) {System.out.print(x.pollFirst() + " " + y.pollFirst() + " ");}System.out.println(x.pollFirst() + " " + y.pollFirst());}
}


http://www.ppmy.cn/embedded/94054.html

相关文章

Windows 11 24H2减少了BitLocker的使用要求 为更多PC打开了自动加密功能

微软的支持代表分享了有关 Windows 11 24H2 如何降低使用 BitLocker 加密的"要求"的详细信息&#xff0c;这意味着现在有更多 PC 可以使用自动和手动加密。这在内部被称为"Auto_DE"&#xff0c;其中的"auto"是自动&#xff0c;而"DE"很…

HTTP的场景实践

HTTP的场景实践&#xff1a;任选一个浏览器&#xff0c;对于其涉及的请求中的缓存策略展开具体分析 1. 强缓存&#xff1a; Cache-Control用于指定缓存的最长有效时间。 Expires用于指定资源过期的日期。 2. 协商缓存&#xff1a; ETag用于标识资源的唯一标识符&#xff0c;…

83.SAP ABAP从前台找字段所在表的两种方法整理笔记

目录 方法1&#xff1a;F1查看技术信息 F1 技术信息 方法2&#xff1a;ST05开启跟踪 Activate Trace Input and save data Deactivate Trace Display Trace 分析你想要的表 方法1&#xff1a;F1查看技术信息 从前台找一个屏幕字段所在表&#xff0c;一般通过按F1来查找…

pod详解 list-watch机制 预选优选策略 如何指定节点调度pod

K8S是通过 list-watch 机制实现每个组件的协同工作 controller-manager、scheduler、kubelet 通过 list-watch 机制监听 apiserver 发出的事件&#xff0c;apiserver 也会监听 etcd 发出的事件 scheduler的调度策略&#xff1a; 预选策略&#xff08;Predicates&#xff09;…

【论文解读】Performance comparison among popular implementations of H.264 encoders

级别:IOP作者:H Y El-Arsh, A S Elliethy, A M Abdelaziz and H A Aly机构:Military Technical College时间:2021下载地址:Performance comparison among popular implementations of H.264 encoders摘要 研究背景: 论文关注由无人机(UAV)携带的高分辨率相机捕获的遥感视…

河南萌新联赛2024第(四)场:河南理工大学 B C D F H I

题目集 B 小雷的神奇电脑 题目描述 小雷有一台特殊的电脑&#xff0c;这台电脑有一个 m m m位的寄存器&#xff0c;能够存储一个 m m m位的二进制数。换句话说&#xff0c;这台电脑可以存储一个从 0 0 0到 2 m − 1 2^m-1 2m−1之间的任何非负整数。 小雷还有一组 n n n个非…

python打怪练习

1. 求一个数的幂值 def mi(a, b):c afor i in range(b-1):a a * creturn aprint(mi(2, 4))2. 输出斐波那契数列 def feibonaqi(n):l []a 1b 1for i in range(n):l.append(a)l.append(b)a b ab a bprint(l)feibonaqi(5)3. 输出特定字典数据 keys [name, old, score…

NVIDIA Studio优化

NVIDIA Studio优化 在生产力方面,NVIDIA GPU打造的CUDA生态圈已经是业界无可匹敌的存在,RTX显卡也是设计师们优先考虑的高效工具,因此RTX40系列配合NVIDIA Studio驱动也将会为内容创作者们提供更加出色的选择。总的来说,目前RTX可以加速110多款主流创意应用,此外,SDK的提…