LeetCode第773题 - 滑动谜题

题目

解答

java 复制代码
class Solution {

    static int V_RIGHT = 1;

    static int V_LEFT = 2;

    static int H_UP = 3;

    static int H_DOWN = 4;

    LinkedList<int[]> queue = new LinkedList<>();

    Set<Integer> visited = new HashSet<>();

    public int slidingPuzzle(int[][] board) {
        int m = board.length;
        int n = board[0].length;

        int[] target = new int[] {1, 2, 3, 4, 5, 0};

        int step = 0;

        int[] init = new int[m * n];
        for (int i = 0, pos = 0; i < m; ++i) {
            for (int j = 0; j < n; ++j, ++pos) {
                init[pos] = board[i][j];
            }
        }

        queue.add(init);
        visited.add(hash(init));

        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; ++i) {
                int[] status = queue.removeFirst();
                if (equals(status, target)) {
                    return step;
                }

                int index = 0;
                for (int j = 0; j < status.length; ++j) {
                    if (status[j] == 0) {
                        index = j;
                        break;
                    }
                }

                moveV(status, n, index, V_LEFT);
                moveV(status, n, index, V_RIGHT);
                moveH(status, n, index, H_UP);
                moveH(status, n, index, H_DOWN);
            }

            ++step;
        }

        return -1;
    }

    int hash(int[] status) {
        int hashCode = 11;
        for (int i = 0; i < status.length; ++i) {
            hashCode = hashCode * 7 + status[i];
        }
        return hashCode;
    }

    boolean equals(int[] status, int[] target) {
        for (int i = 0; i < status.length; ++i) {
            if (status[i] != target[i]) {
                return false;
            }
        }

        return true;
    }

    void moveV(int[] status, int column, int index, int flag) {
        int[] newStatus = Arrays.copyOf(status, status.length);
        // 和右边交换。
        if (flag == V_RIGHT) {

            if (index % column + 1 < column) {
                newStatus[index] = newStatus[index + 1];
                newStatus[index + 1] = 0;
            }
        }
        // 和左边交换
        else if (flag == V_LEFT) {
            if (index % column - 1 >= 0) {
                newStatus[index] = newStatus[index - 1];
                newStatus[index - 1] = 0;
            }
        }

        int hashCode = hash(newStatus);

        if (visited.add(hashCode)) {
            queue.add(newStatus);
        }
    }

    void moveH(int[] status, int column, int index, int flag) {
        int[] newStatus = Arrays.copyOf(status, status.length);
        if (flag == H_UP) {
            if (index - column >= 0) {
                newStatus[index] = newStatus[index - column];
                newStatus[index - column] = 0;
            }
        } else if (flag == H_DOWN) {
            if (index + column < status.length) {
                newStatus[index] = newStatus[index + column];
                newStatus[index + column] = 0;
            }
        }

        int hashCode = hash(newStatus);

        if (visited.add(hashCode)) {
            queue.add(newStatus);
        }
    }

}

总结

基于广度优先算法求解。

要点:

  • 将输入的多维数组转换为一维数组,计算指定位置的相邻节点。
  • 去重的方法。
相关推荐
Eward-an20 分钟前
LeetCode 239. 滑动窗口最大值(详细技术解析)
python·算法·leetcode
一叶落43835 分钟前
LeetCode 50. Pow(x, n)(快速幂详解 | C语言实现)
c语言·算法·leetcode
皙然39 分钟前
彻底吃透红黑树
数据结构·算法
t198751281 小时前
TOA定位算法MATLAB实现(二维三维场景)
开发语言·算法·matlab
jllllyuz1 小时前
粒子群算法解决资源分配问题的MATLAB实现
开发语言·算法·matlab
renhongxia11 小时前
从模仿到创造:具身智能的技能演化路径
人工智能·深度学习·神经网络·算法·机器学习·知识图谱
qq_401700412 小时前
顺序、二分、插值、斐波那契查找算法
数据结构·算法·排序算法
x_xbx2 小时前
LeetCode:26. 删除有序数组中的重复项
数据结构·算法·leetcode
WitsMakeMen2 小时前
RoPE 算法原理?算法为什么只和相对位置有关
人工智能·算法·llm
0 0 02 小时前
CCF-CSP 38-4 月票发行【C++】考点:动态规划DP+矩阵快速幂
c++·算法·动态规划·矩阵快速幂