华为OD机试真题E卷 - 跳马 (Java/JS/Py/C/C++)

题目描述

马是象棋(包括中国象棋和国际象棋)中的棋子,走法是每步直一格再斜一格,即先横着或者直者走一格,然后再斜着走一个对角线,可进可退,可越过河界,俗称"马走日"字。给定 m 行 n 列的棋盘(网格图),棋盘上只有象棋中的棋子“马”,并且每个棋子有等级之分,等级为 k 的马可以跳 1~k 步(走的方式与象棋中“马”的规则一样,不可以超出棋盘位置),问是否能将所有马跳到同一位置,如果存在,输出最少需要的总步数(每匹马的步数相加),不存在则输出-1。

注:允许不同的马在跳的过程中跳到同一位置,坐标为(x,y)的马跳一次可以跳到的坐标为:(x+1, y+2),(x+1, y-2),(x+2, y+1),(x+2, y-1),(x-1, y+2),(x-1, y-2),(x-2, y+1),(x-2, y-1)的格点上,但是不可以超出棋盘范围。

输入描述

第一行输入m,n,代表 m 行 n 列的网格图棋盘(1 ≤ m, n ≤ 25)

接下来输入 m 行 n 列的网格图棋盘,如果第 i 行,第 j 列的元素为 “.” ,代表此格点没有棋子,如果为数字 k(1 ≤ k ≤ 9),代表此格点存在等级为 k 的“马”。

输出描述

输出最少需要的总步数(每匹马的步数相加),不存在则输出-1。

示例1

输入

3 2
..
2 .
..

输出

0

说明:只有一匹马,不需要跳动

示例2

输入

3 5
47.48
4744.
7....

输出

17

备注

解题思路

源码

Java语言源码

public class Main {

    static class Node {
        int x;
        int y;
        int step;

        public Node(int x, int y, int step) {
            this.x = x;
            this.y = y;
            this.step = step;
        }
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 构建棋盘
        String[] split = in.nextLine().split(" ");
        int m = Integer.parseInt(split[0]);
        int n = Integer.parseInt(split[1]);
        char[][] chess = new char[m][n];
        for (int i = 0; i < m; i++) {
            chess[i] = in.nextLine().toCharArray();
        }
        // 马的位置和可走的最大步数
        LinkedList<Node> list = new LinkedList<>();
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (chess[i][j] != '.') {
                    Node node = new Node(i, j, chess[i][j] - '0');
                    list.add(node);
                }
            }
        }

        // 最小步数
        int minSteps = Integer.MAX_VALUE;
        // 马能走的方向
        int[][] direction = {{1, 2}, {1, -2}, {2, 1}, {2, -1}, {-1, 2}, {-1, -2}, {-2, 1}, {-2, -1}};

        // 遍历棋盘
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                // 所有马到达
                boolean allFlag = true;
                // 步数
                int step = 0;
                // 遍历马
                for (Node node : list) {
                    // 记录马的位置和步数
                    LinkedList<Node> queue = new LinkedList<>();
                    queue.add(new Node(node.x, node.y, 0));
                    // 记录已经访问过的位置
                    boolean[][] vis = new boolean[m][n];
                    vis[node.x][node.y] = true;

                    boolean found = false;
                    while (!queue.isEmpty() && allFlag) {
                        Node poll = queue.poll();
                        if (poll.x == i && poll.y == j) {
                            found = true;
                            step += poll.step;
                            break;
                        }

                        //马走八方
                        for (int[] ints : direction) {
                            int newX = poll.x + ints[0];
                            int newY = poll.y + ints[1];

                            if (newX >= 0 && newX < m && newY >= 0 && newY < n && !vis[newX][newY]) {
                                queue.offer(new Node(newX, newY, poll.step + 1));
                                vis[newX][newY] = true;
                            }
                        }
                    }
                    if (!found) {
                        allFlag = false;
                    }
                }
                if (allFlag) {
                    minSteps = Math.min(minSteps, step);
                }
            }
        }
        if (minSteps == Integer.MAX_VALUE) {
            System.out.println(-1);
        } else {
            System.out.println(minSteps);
        }
    }
}

JavaScript语言源码

在这里插入JavaScript代码片

Python语言源码

在这里插入Python代码片

C语言源码

在这里插入C代码片

C++语言源码

在这里插入C++代码片

你可能感兴趣的:(2025最新华为OD机试题目,华为od)