news 2026/4/4 0:22:21

(新卷,100分)- 多段线数据压缩(Java JS Python C)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
(新卷,100分)- 多段线数据压缩(Java JS Python C)

(新卷,100分)- 多段线数据压缩(Java & JS & Python & C)

题目描述

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

为简化处理,多线段的走向只能是水平、竖直、斜向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

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

备注

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

用例
输入2 8 3 7 3 6 3 5 4 4 5 3 6 2 7 3 8 4 7 5
输出2 8 3 7 3 5 6 2 8 4 7 5
说明

如上图所示,6个蓝色像素的坐标依次是:

(2, 8)、(3, 7)、(3, 5)、(6, 2)、(8, 4)、(7, 5)

将他们按顺序输出即可。

题目解析

本题其实就是要我们保留拐点。

那么怎么判断一个点是不是拐点呢?

拐点拐点,自然是运动方向发生改变的点。

那么如何判断一个点的运动方向呢?

运动,指的是从点A到点B,而运动的方向,自然是点A到点B的方向。那么如何描述点A到点B的方向呢?

假设点A(2, 8),点B(3 7),那么此时点A到点B的偏移量为:

offsetX = 3 - 2 = 1

offsetY = 7 - 8 = -1

则(offsetX, offsetY)就是A→B的向量坐标。

当然还有可能出现这样的情况,比如A坐标(3,5),B坐标(6,2),此时A→B向量坐标为(3, -3)

此时(3,-3)向量的方向其实和(1,-1)是相同的。

因此,我们需要对这种向量做简化,方便后续相同方向的比较,即将(3,-3)简化为(1,-1),字面上看,其实就是横坐标、纵坐标都除以3,那么base=3该如何求解呢?求解公式如下

base = max(abs(offsetX), abs(offsetY))

这个公式的好处是,兼容(-5, 0),(0, 10)这种向量。

当我们知道了A→B的方向,那么只要判断下一步B→C的方向是否发生改变,如果发生改变,那么就可以确定B是拐点,比如

A→B的向量为(-1, 0)

B→C的向量为(-1, 0)

因此B不是拐点

B→C的向量为(-1, 0)

C→D的向量为(1, -1)

因此C是拐点

JS算法源码
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { const nums = (await readline()).split(" ").map(Number); console.log(getResult(nums)); })(); function getResult(nums) { const ans = []; // 上一个点坐标(preX, preY) let preX = nums[0]; let preY = nums[1]; // 上一次的运动方向(preDirectX, preDirectY) let preDirectX = 0; let preDirectY = 0; for (let i = 2; i < nums.length; i += 2) { // 当前点坐标(curX, curY) const curX = nums[i]; const curY = nums[i + 1]; // 上一个点到当前点的偏移量(offsetX, offsetY) const offsetX = curX - preX; const offsetY = curY - preY; // 根据偏移量得出本次的运动方向 const base = Math.max(Math.abs(offsetX), Math.abs(offsetY)); const directX = offsetX / base; const directY = offsetY / base; // 如果两次运动的方向不同 if (directX != preDirectX || directY != preDirectY) { // 则上一个点是拐点,需要记录下来 ans.push(preX, preY); } preX = curX; preY = curY; preDirectX = directX; preDirectY = directY; } // 注意收尾 ans.push(preX, preY); return ans.join(" "); }
Java算法源码
import java.util.Arrays; import java.util.Scanner; import java.util.StringJoiner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] nums = Arrays.stream(sc.nextLine().split(" ")).mapToInt(Integer::parseInt).toArray(); System.out.println(getResult(nums)); } public static String getResult(int[] nums) { StringJoiner sj = new StringJoiner(" "); // 上一个点坐标(preX, preY) int preX = nums[0]; int preY = nums[1]; // 上一次的运动方向(preDirectX, preDirectY) int preDirectX = 0; int preDirectY = 0; for (int i = 2; i < nums.length; i += 2) { // 当前点坐标(curX, curY) int curX = nums[i]; int curY = nums[i + 1]; // 上一个点到当前点的偏移量(offsetX, offsetY) int offsetX = curX - preX; int offsetY = curY - preY; // 根据偏移量得出本次的运动方向 int base = Math.max(Math.abs(offsetX), Math.abs(offsetY)); int directX = offsetX / base; int directY = offsetY / base; // 如果两次运动的方向不同 if (directX != preDirectX || directY != preDirectY) { // 则上一个点是拐点,需要记录下来 sj.add(preX + " " + preY); } preX = curX; preY = curY; preDirectX = directX; preDirectY = directY; } // 注意收尾 sj.add(preX + " " + preY); return sj.toString(); } }
Python算法源码
# 输入获取 nums = list(map(int, input().split())) # 算法入口 def getResult(): ans = [] # 上一个点坐标(preX, preY) preX = nums[0] preY = nums[1] # 上一次的运动方向(preDirectX, preDirectY) preDirectX = 0 preDirectY = 0 for i in range(2, len(nums), 2): # 当前点坐标(curX, curY) curX = nums[i] curY = nums[i + 1] # 上一个点到当前点的偏移量(offsetX, offsetY) offsetX = curX - preX offsetY = curY - preY # 根据偏移量得出本次的运动方向 base = max(abs(offsetX), abs(offsetY)) directX = offsetX // base directY = offsetY // base # 如果两次运动的方向不同 if directX != preDirectX or directY != preDirectY: # 则上一个点是拐点,需要记录下来 ans.extend([preX, preY]) preX = curX preY = curY preDirectX = directX preDirectY = directY # 注意收尾 ans.extend([preX, preY]) return " ".join(map(str, ans)) # 算法调用 print(getResult())
C算法源码
#include <stdio.h> #include <math.h> #define MAX_SIZE 20000 int main() { int nums[MAX_SIZE]; int nums_size = 0; while (scanf("%d", &nums[nums_size++])) { if (getchar() != ' ') break; } // 上一个点坐标(preX, preY) int preX = nums[0]; int preY = nums[1]; // 上一次的运动方向(preDirectX, preDirectY) int preDirectX = 0; int preDirectY = 0; for (int i = 2; i < nums_size; i += 2) { // 当前点坐标(curX, curY) int curX = nums[i]; int curY = nums[i + 1]; // 上一个点到当前点的偏移量(offsetX, offsetY) int offsetX = curX - preX; int offsetY = curY - preY; // 根据偏移量得出本次的运动方向 int base = (int) fmax(abs(offsetX), abs(offsetY)); int directX = offsetX / base; int directY = offsetY / base; // 如果两次运动的方向不同 if (directX != preDirectX || directY != preDirectY) { // 则上一个点是拐点,需要记录下来 printf("%d %d ", preX, preY); } preX = curX; preY = curY; preDirectX = directX; preDirectY = directY; } // 注意收尾 printf("%d %d", preX, preY); return 0; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/3/15 17:25:37

YOLO26创新改进 | 全网独家创新、注意力改进篇 | ICCV 2025 | 引入超强MALA幅度感知线性注意力模块,多种创新改进,助力目标检测、图像分类、实例分割高效涨点

一、本文介绍 本文给大家介绍一种超强MALA幅度感知线性注意力模块优化YOLO26模型!超强MALA幅度感知线性注意力模块,显著超越了现有线性注意力机制及部分Softmax注意力模型,增强目标检测、图像分类、实例分割暴力涨点 。具体怎么使用请看全文! 🔥欢迎订阅我的专栏、带你…

作者头像 李华
网站建设 2026/4/1 20:11:24

clawdbot 云服务器部署

clawdbot clawdbot设置密码访问连接报错配对设备参考 clawdbot nvm install 22 nvm alias default 22设置密码访问 curl -fsSL https://molt.bot/install.sh | bashclawdbot configure --section gateway┌ Clawdbot configure │ ◇ Existing config detected ─╮ │ …

作者头像 李华
网站建设 2026/3/27 16:48:50

**AI漫剧生成工具2025推荐,解锁高效创作与视觉叙事新体

AI漫剧生成工具2025推荐&#xff0c;解锁高效创作与视觉叙事新体验在内容创作领域&#xff0c;一场由AI驱动的视觉叙事革命正在加速。据《2025中国数字内容产业白皮书》显示&#xff0c;2025年AI生成视频内容的市场需求同比增长超过300%&#xff0c;其中AI漫剧因其独特的视觉风…

作者头像 李华
网站建设 2026/3/27 11:08:09

Linux下串口波特率配置命令操作指南

以下是对您提供的博文内容进行 深度润色与结构优化后的技术文章 。整体遵循“去AI化、强工程感、重逻辑流、轻模板化”的原则,彻底摒弃引言/总结等程式化段落,以一位嵌入式Linux一线工程师的口吻娓娓道来——既有踩坑后的顿悟,也有调试时的真实节奏;既讲清楚“怎么做”,…

作者头像 李华
网站建设 2026/3/27 15:32:23

Qwen3-4B惊艳效果展示:多语言代码注释自动生成(含中文)

Qwen3-4B惊艳效果展示&#xff1a;多语言代码注释自动生成&#xff08;含中文&#xff09; 1. 开场&#xff1a;一段代码&#xff0c;三秒加注释&#xff0c;中英法德日全搞定 你有没有过这样的时刻&#xff1a;翻出半年前写的Python脚本&#xff0c;第一行就卡住——这函数到…

作者头像 李华