zoukankan      html  css  js  c++  java
  • [LintCode] 794. Sliding Puzzle II

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

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

    Given an initial state of the puzzle board and final state, return the least number of moves required so that the initial state to final state.

    If it is impossible to move from initial state to final state, return -1.

    Example

    Example 1:

    Input:
    [
     [2,8,3],
     [1,0,4],
     [7,6,5]
    ]
    [
     [1,2,3],
     [8,0,4],
     [7,6,5]
    ]
    Output:
    4
    
    Explanation:
    [                 [
     [2,8,3],          [2,0,3],
     [1,0,4],   -->    [1,8,4],
     [7,6,5]           [7,6,5]
    ]                 ]
    
    [                 [
     [2,0,3],          [0,2,3],
     [1,8,4],   -->    [1,8,4],
     [7,6,5]           [7,6,5]
    ]                 ]
    
    [                 [
     [0,2,3],          [1,2,3],
     [1,8,4],   -->    [0,8,4],
     [7,6,5]           [7,6,5]
    ]                 ]
    
    [                 [
     [1,2,3],          [1,2,3],
     [0,8,4],   -->    [8,0,4],
     [7,6,5]           [7,6,5]
    ]                 ]
    

    Example 2:

    Input:
    [[2,3,8],[7,0,5],[1,6,4]]
    [[1,2,3],[8,0,4],[7,6,5]]
    Output:
    -1


    public class Solution {
        /**
         * @param init_state: the initial state of chessboard
         * @param final_state: the final state of chessboard
         * @return: return an integer, denote the number of minimum moving
         */
        public int minMoveStep(int[][] init_state, int[][] final_state) {
            // # write your code here
            String start = matrixToStr(init_state);
            String end = matrixToStr(final_state);
            int step = 0;
            Set<String> visited = new HashSet<>();
            Queue<String> queue = new LinkedList<>();
            queue.offer(start);
            visited.add(start);
            while (!queue.isEmpty()) {
                int size = queue.size();
                while (size-- > 0) {
                    String cur = queue.poll();
                    if (cur.equals(end)) {
                        return step;
                    }
                    for (String nxt: getNext(cur)) {
                        if (visited.contains(nxt)) {
                            continue;
                        }
                        queue.offer(nxt);
                        visited.add(nxt);
                    }
                }
                step += 1;
            }
            return -1;
        }
        
        private String matrixToStr(int[][] matrix) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < matrix.length; i++) {
                for (int j = 0; j < matrix[0].length; j++) {
                    sb.append(matrix[i][j]);
                }
            }
            return sb.toString();
        }
        
        private List<String> getNext(String cur) {
            List<String> list = new ArrayList<>();
            int zeroIdx = cur.indexOf("0");
            int curRow = zeroIdx / 3;
            int curCol = zeroIdx % 3;
            int[][] directions = new int[][]{{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
            for (int[] direction : directions) {
                int nxtRow = curRow + direction[0];
                int nxtCol = curCol + direction[1];
                if (nxtRow < 0 || nxtRow >= 3 || nxtCol < 0 || nxtCol >= 3) {
                    continue;
                }
                char[] charArr = cur.toCharArray();
                charArr[zeroIdx] = charArr[nxtRow * 3 + nxtCol];
                charArr[nxtRow * 3 + nxtCol] = '0';
                list.add(new String(charArr));
            }
            return list;
        }
    }
  • 相关阅读:
    PHP多台服务器跨域SESSION共享
    php发送post请求到nodejs服务器
    xampp使用phpunit
    MarkdownPad 2
    php安装memcache注意事项
    yii 基础版用rbac-plus
    yii2高级版账号密码问题
    yii2 rbac-plus的使用
    manjaro-VM虚拟机vmmon错误
    Java并发包中的线程池ThreadPoolExecutor
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12571964.html
Copyright © 2011-2022 走看看