• LeetCode 773. Sliding Puzzle


    原题链接在这里:https://leetcode.com/problems/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].

    题解:

    题目说道least number of moves, 应该想到用BFS.

    Queue里面放上现在board的状态, 利用新的类Node, 记录board, 0所在位置和查询的深度.

    poll时如果出现了target状态,就找到了结果, 返回深度.

    BFS用Set来保存出现过的状态. Serialize the board to string.

    Time Complexity: O((m*n)!*m*n). m = board.length, n = board[0].length. board一共有(m*n)! 种可能状态, 也就是queue的可能长度. 处理queue的每个node用时O(m*n).

    Space: O((m*n)!).

    AC Java:

     1 class Solution {
     2     public int slidingPuzzle(int[][] board) {
     3         int m = 2;
     4         int n = 3;
     5         
     6         String target = "123450";
     7         String start = "";
     8         for(int i = 0; i < m; i++){
     9             for(int j = 0; j < n; j++){
    10                 start = start + board[i][j];
    11             }
    12         }
    13         
    14         HashSet<String> visited = new HashSet<>();
    15         visited.add(start);
    16         LinkedList<String> que = new LinkedList<>();
    17         que.add(start);
    18         int level = 0;
    19         
    20         int [][] dirs = new int[][]{{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
    21         while(!que.isEmpty()){
    22             int size = que.size();
    23             while(size-- > 0){
    24                 String cur = que.poll();
    25                 if(cur.equals(target)){
    26                     return level;
    27                 }
    28                 
    29                 int ind = cur.indexOf('0');
    30                 int r = ind / n;
    31                 int c = ind % n;
    32                 for(int [] dir : dirs){
    33                     int x = r + dir[0];
    34                     int y = c + dir[1];
    35                     if(x < 0 || x >= m || y < 0 || y >= n){
    36                         continue;
    37                     }
    38                     
    39                     int ind1 = x * n + y;
    40                     StringBuilder sb = new StringBuilder(cur);
    41                     sb.setCharAt(ind, cur.charAt(ind1));
    42                     sb.setCharAt(ind1, cur.charAt(ind));
    43                     String can = new String(sb);
    44                     
    45                     if(visited.contains(can)){
    46                         continue;
    47                     }
    48                     
    49                     visited.add(can);
    50                     que.add(can);
    51                 }
    52             }
    53             
    54             level++;
    55         }
    56         
    57         return -1;
    58     }
    59 }
  • 相关阅读:
    log4cpp
    互斥锁封装
    Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)
    Codeforces 920E-Connected Components? (set,补图,连通块)
    Persistent Bookcase CodeForces
    P4390 [BOI2007]Mokia 摩基亚 (CDQ解决三维偏序问题)
    P3157 [CQOI2011]动态逆序对 (CDQ解决三维偏序问题)
    CDQ 分治解决和点对有关的问题
    洛谷 P2163 [SHOI2007]园丁的烦恼 (离线sort,树状数组,解决三维偏序问题)
    洛谷 P3469 [POI2008]BLO-Blockade (Tarjan,割点)
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/9384169.html
Copyright © 2020-2023  润新知