2025.04.23华为暑期实习真题【图像亮度坐标搜索】Java/Python/C++/JS/C 实现

目录

题目

思路

Code


题目

题目内容
给定一张二维图像,图像中每个值表示该坐标下的亮度。现在给定一个亮度值m ,请返回离图像中心坐标最近的k个亮度为 m 值的坐标(x ,y)。
提示:
1.图像中元素的坐标范围æ:[0,w-1],y:[0,h-1]。
2.图像宽高 w,h 均为奇数,图像中心坐标(w - 1)/2,(h-1)/2
3.平面上两点之间的距离为|x1- x2|+|y1 - y2|。
4.在距离相同的情况下,以x小的点优先;当x 相同时,以y小的点优先。
5.题目可保证至少存在一个亮度值为 m 的点。

输入描述
第一行输入为图像宽度 w 和图像高度 h,以空格隔开,宽高范围为 1~ 2000
第二行输入为给定亮度值 m,范围为 1~1000
第三行输入为需要输出的亮度值为 m 的坐标个数k,范围为1~100 ,且k<= w*h
接下来共 h 行,每行内为 w 个亮度,以空格隔开,亮度范围为 1~1000

输出描述
按顺序输出坐标序列,坐标:和y以空格隔开,坐标间使用空格隔开如样例所示。
,y坐标均不相同时以。增序排列,坐标相同时以y增序排列。若满足条件的坐标个数不足k,则以实际坐标个数输出。

示例1:
输入:
5 5
10
3
10 2 3 4 5
1 2 3 4 10
1 2 3 10 5
1 10 3 4 5
1 2 3 4 5
输出:
3 2 1 3 4 1
说明
该用例图像w=5,h=5,给定亮度值 m=10,需要输出 m = 10 的距离中心最近的 3个坐标。
该图像中心坐标为(2,2)。
从图像中可以看出,距离中心坐标最近的m=10 的坐标为(3,2),距离为|3-2|+|2-2|=1;
距离第二近的坐标为(1,3),距离为|1-2|+|3-2|=2;
距离第三近的坐标为(4,1),距离为|4-2|+|1-2|=3;
由上可知,三个坐标为(3,2)、(1,3)、(4,1),按照输出格式要求,输出为 321341


示例2:
输入:
3 3
10
6
1 10 1
10 10 10
1 10 1
输出:
1 1 0 1 1 0 1 2 2 1
说明
该用例图像w=3,h=3,给定亮度值 m=10,需要输出 m = 10 的距离中心最近的 6个坐标。
该图像中心坐标为(1,1)。
从图像中可以看出,距离中心坐标最近的 m=10 的坐标为(1,1),距离为 0;
剩余满足 m = 10 的坐标有 4个,分别为(1,0)(0,1)(2,1)(1,2),距离均为 1
由输出规则 …,y坐标均不相同时以x增序排列,:坐标相同时以y增序排列,得到输出序列为(1,1)(0,1)(1,0)(1,2)(2,1),
满足条件的坐标为5个,不足输入中要求的6个,实际只需输入5个。
由上可知,输出为 1101101221

思路

  • 读入输入数据:图像宽高w, h,亮度值m,需要输出的数量k,以及图像本身。

  • 确定中心点坐标center_x = (w-1)//2, center_y = (h-1)//2

  • 遍历整个图像,找到所有亮度为m的点,记录成一个列表,元素是:(x坐标, y坐标)。

  • 计算每个点到中心点的距离,记录成 (距离, x, y) 的元组。

  • 按照 (距离, x, y) 排序

    • 先按距离升序排

    • 再按x升序

    • 再按y升序

  • 取前k个元素(如果不足k个,取全部)。

  • 输出格式化结果

  • 遍历一次图像找点:O(w*h)

  • 排序找到最近的点:如果找到p个目标点,排序是O(p*log(p))

  • p最大是w*h,但通常远小于。

所以总体时间复杂度大约是 O(w*h + p*log(p))

Python

# 读取输入
w, h = map(int, input().split())
m = int(input())
k = int(input())

# 读取图像
image = []
for _ in range(h):
    image.append(list(map(int, input().split())))

# 计算中心坐标
center_x = (w - 1) // 2
center_y = (h - 1) // 2

# 收集所有亮度为m的点
points = []
for y in range(h):
    for x in range(w):
        if image[y][x] == m:
            # 曼哈顿距离 + 坐标
            dist = abs(x - center_x) + abs(y - center_y)
            points.append((dist, x, y))

# 按距离、x、y排序
points.sort()

# 取前k个(如果不足k个,取实际数量)
result = points[:k]

# 格式化输出
output = []
for _, x, y in result:
    output.append(str(x))
    output.append(str(y))

print(' '.join(output))

 Java

import java.util.*;

public class Main {
    static class Point implements Comparable {
        int dist, x, y;
        public Point(int dist, int x, int y) {
            this.dist = dist;
            this.x = x;
            this.y = y;
        }
        public int compareTo(Point other) {
            if (this.dist != other.dist)
                return this.dist - other.dist;
            if (this.x != other.x)
                return this.x - other.x;
            return this.y - other.y;
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int w = sc.nextInt();
        int h = sc.nextInt();
        int m = sc.nextInt();
        int k = sc.nextInt();

        int[][] image = new int[h][w];
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                image[i][j] = sc.nextInt();
            }
        }

        int centerX = (w - 1) / 2;
        int centerY = (h - 1) / 2;
        List points = new ArrayList<>();

        for (int y = 0; y < h; y++) {
            for (int x = 0; x < w; x++) {
                if (image[y][x] == m) {
                    int dist = Math.abs(x - centerX) + Math.abs(y - centerY);
                    points.add(new Point(dist, x, y));
                }
            }
        }

        Collections.sort(points);

        StringBuilder sb = new StringBuilder();
        int limit = Math.min(k, points.size());
        for (int i = 0; i < limit; i++) {
            sb.append(points.get(i).x).append(" ").append(points.get(i).y).append(" ");
        }
        System.out.println(sb.toString().trim());
    }
}

 C++

#include 
#include 
#include 
#include 
#include 
using namespace std;

// 定义一个结构体表示点
struct Point {
    int dist, x, y;
    bool operator<(const Point& other) const {
        if (dist != other.dist) return dist < other.dist;
        if (x != other.x) return x < other.x;
        return y < other.y;
    }
};

int main() {
    int w, h, m, k;
    cin >> w >> h;
    cin >> m;
    cin >> k;
    vector> image(h, vector(w));

    for (int i = 0; i < h; ++i)
        for (int j = 0; j < w; ++j)
            cin >> image[i][j];

    int center_x = (w - 1) / 2;
    int center_y = (h - 1) / 2;

    vector points;
    for (int y = 0; y < h; ++y) {
        for (int x = 0; x < w; ++x) {
            if (image[y][x] == m) {
                int dist = abs(x - center_x) + abs(y - center_y);
                points.push_back({dist, x, y});
            }
        }
    }

    sort(points.begin(), points.end());

    int limit = min(k, (int)points.size());
    for (int i = 0; i < limit; ++i) {
        cout << points[i].x << " " << points[i].y << " ";
    }
    return 0;
}

 JavaScript

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let lines = [];

rl.on('line', function(line) {
    lines.push(line.trim());
});

rl.on('close', function() {
    const [w, h] = lines[0].split(' ').map(Number);
    const m = Number(lines[1]);
    const k = Number(lines[2]);
    const image = lines.slice(3).map(line => line.split(' ').map(Number));

    const centerX = (w - 1) >> 1;
    const centerY = (h - 1) >> 1;

    let points = [];
    for (let y = 0; y < h; y++) {
        for (let x = 0; x < w; x++) {
            if (image[y][x] === m) {
                const dist = Math.abs(x - centerX) + Math.abs(y - centerY);
                points.push([dist, x, y]);
            }
        }
    }

    points.sort((a, b) => {
        if (a[0] !== b[0]) return a[0] - b[0];
        if (a[1] !== b[1]) return a[1] - b[1];
        return a[2] - b[2];
    });

    let result = [];
    for (let i = 0; i < Math.min(k, points.length); i++) {
        result.push(points[i][1], points[i][2]);
    }

    console.log(result.join(' '));
});

 C语言

#include 
#include 

typedef struct {
    int dist, x, y;
} Point;

int cmp(const void* a, const void* b) {
    Point* pa = (Point*)a;
    Point* pb = (Point*)b;
    if (pa->dist != pb->dist) return pa->dist - pb->dist;
    if (pa->x != pb->x) return pa->x - pb->x;
    return pa->y - pb->y;
}

int main() {
    int w, h, m, k;
    scanf("%d %d", &w, &h);
    scanf("%d", &m);
    scanf("%d", &k);

    int image[h][w];
    for (int i = 0; i < h; i++)
        for (int j = 0; j < w; j++)
            scanf("%d", &image[i][j]);

    int center_x = (w - 1) / 2;
    int center_y = (h - 1) / 2;

    Point points[w * h];
    int cnt = 0;

    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            if (image[y][x] == m) {
                points[cnt].dist = abs(x - center_x) + abs(y - center_y);
                points[cnt].x = x;
                points[cnt].y = y;
                cnt++;
            }
        }
    }

    qsort(points, cnt, sizeof(Point), cmp);

    int limit = k < cnt ? k : cnt;
    for (int i = 0; i < limit; i++) {
        printf("%d %d ", points[i].x, points[i].y);
    }

    return 0;
}

【华为od机试真题Python+JS+Java合集】【超值优惠】:Py/JS/Java合集

【华为od机试真题Python】:Python真题题库

【华为od机试真题JavaScript】:JavaScript真题题库

【华为od机试真题Java】:Java真题题库

【华为od机试真题C++】:C++真题题库

【华为od机试真题C语言】:C语言真题题库

【华为od面试手撕代码题库】:面试手撕代码题库

【华为od机试面试交流群:830285880】

华为OD机试:二本院校有机会吗?
有机会,但不大,大神除外!机考分数越高越好,所以需要提前刷题。机考通过后,如果没有收到面试邀请,也不要着急,非目标院校面试邀请发的时间比较晚。非目标院校今年有点难,机试至少要考到350分,所以需要疯狂刷题,华为OD机考是有题库的,最好在考前完所有题库题目。华为OD机试:跨专业可以参加华为OD可以,但是如果你的本科院校比较差,上岸概率不大。华为OD机试:华为OD简历被锁定机试通过,性格测试也通过,但是没人联系面试,发现简历被锁定。此时需要主动去联系HR。让他帮助你查询原因。

你可能感兴趣的:(python,华为,java,华为暑期实习机试,开发语言,c++)