zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    Apache虚拟目录的建立
    自制户外登山地图傻瓜书
    经纬度与高克投影转换代码(VB)
    2000国家大地坐标系
    js格式化 Thu Mar 07 2019 12:00:00 GMT+0800 (中国标准时间) 及相互转化
    Javascript农历与公历相互转换
    Numpy
    日期多选插件Kalendae.js
    Scrapy项目实战
    bootstrapdatetimepicker添加支持显示农历节假日信息。
  • 原文地址:https://www.cnblogs.com/Dylan-Java-NYC/p/9384169.html
Copyright © 2011-2022 走看看