• [LeetCode] 773. Sliding Puzzle


    On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.

    A move consists of choosing 0 and a 4-directionally adjacent number and swapping it.

    The state of the board is solved if and only if the board is [[1,2,3],[4,5,0]].

    Given a puzzle board, return the least number of moves required so that the state of the board is solved. If it is impossible for the state of the board to be solved, return -1.

    Examples:

    Input: board = [[1,2,3],[4,0,5]]
    Output: 1
    Explanation: Swap the 0 and the 5 in one move.
    
    Input: board = [[1,2,3],[5,4,0]]
    Output: -1
    Explanation: No number of moves will make the board solved.
    
    Input: board = [[4,1,2],[5,0,3]]
    Output: 5
    Explanation: 5 is the smallest number of moves that solves the board.
    An example path:
    After move 0: [[4,1,2],[5,0,3]]
    After move 1: [[4,1,2],[0,5,3]]
    After move 2: [[0,1,2],[4,5,3]]
    After move 3: [[1,0,2],[4,5,3]]
    After move 4: [[1,2,0],[4,5,3]]
    After move 5: [[1,2,3],[4,5,0]]
    
    Input: board = [[3,2,4],[1,5,0]]
    Output: 14
    

    Note:

    • board will be a 2 x 3 array as described above.
    • board[i][j] will be a permutation of [0, 1, 2, 3, 4, 5].

    滑动谜题。

    在一个 2 x 3 的板上(board)有 5 块砖瓦,用数字 1~5 来表示, 以及一块空缺用 0 来表示.

    一次移动定义为选择 0 与一个相邻的数字(上下左右)进行交换.

    最终当板 board 的结果是 [[1,2,3],[4,5,0]] 谜板被解开。

    给出一个谜板的初始状态,返回最少可以通过多少次移动解开谜板,如果不能解开谜板,则返回 -1 。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/sliding-puzzle
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    这个题很像华容道。题目最终的目的是要将0-5这六个数字经过两两交换,变化成[[1, 2, 3], [4, 5, 0]]的样子,请你return的是做到这样需要动几步。红色的例子我再用excel表达一下过程吧。

    既然是问最少需要挪动几步,所以会想到用BFS。我这里直接引用LC discussion的最高票答案,思路直接,同时有一些细节实现非常巧妙。首先将start状态记录成string,跟target作比较;然后13行的dirs,是一个所有数字能被swap到的位置的可能,比如如果你在位置1,你可以被swap到位置3,如果你在位置0,你可以被swap到位置2或者位置4,以此类推。

    时间O(6!) - 因为这里最多就只有6!种可能,类似 permutation

    空间O(n) - hashset + queue

    Java实现

     1 class Solution {
     2     public int slidingPuzzle(int[][] board) {
     3         String start = "";
     4         String target = "123450";
     5         for (int i = 0; i < board.length; i++) {
     6             for (int j = 0; j < board[0].length; j++) {
     7                 start += board[i][j];
     8             }
     9         }
    10         HashSet<String> visited = new HashSet<>();
    11         // 0 1 2
    12         // 3 4 5
    13         int[][] dirs = new int[][] {
    14             {1, 3}, {0, 2, 4}, {1, 5},
    15             {0, 4}, {1, 3, 5}, {2, 4}
    16         };
    17         Queue<String> queue = new LinkedList<>();
    18         queue.offer(start);
    19         visited.add(start);
    20         int res = 0;
    21         while (!queue.isEmpty()) {
    22             int size = queue.size();
    23             for (int i = 0; i < size; i++) {
    24                 // 123
    25                 // 405
    26                 String cur = queue.poll();
    27                 if (cur.equals(target)) {
    28                     return res;
    29                 }
    30                 // zero = 4, which is 1 3 5
    31                 int zero = cur.indexOf('0');
    32                 for (int dir : dirs[zero]) {
    33                     String next = swap(cur, zero, dir);
    34                     if (visited.contains(next)) {
    35                         continue;
    36                     }
    37                     visited.add(next);
    38                     queue.offer(next);
    39                 }
    40             }
    41             res++;
    42         }
    43         return -1;
    44     }
    45 
    46     private String swap(String str, int i, int j) {
    47         StringBuilder sb = new StringBuilder(str);
    48         sb.setCharAt(i, str.charAt(j));
    49         sb.setCharAt(j, str.charAt(i));
    50         return sb.toString();
    51     }
    52 }

    LeetCode 题目总结

  • 相关阅读:
    SpringMVC源码阅读-通过画图理解初始化(十一)
    通过Stratus 服务器在Flash Player中使用RTMFP 开发P2P应用
    Lucene 学习资料
    LIRe 源代码分析 3:基本接口(ImageSearcher)
    LIRe 源代码分析 2:基本接口(DocumentBuilder)
    LIRe 源代码分析 1:整体结构
    Media Player Classic
    ffdshow 源代码分析 5: 位图覆盖滤镜(总结)
    ITU-T Technical Paper: 测量QoS的基本网络模型
    ITU-T Technical Paper: QoS 测量 (目标,方法,协议)
  • 原文地址:https://www.cnblogs.com/cnoodle/p/12764964.html
Copyright © 2020-2023  润新知