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;
        }
    }
  • 相关阅读:
    《卓有成效的管理者》读后感
    小课堂week13 Clean Code Part2
    小课堂Week12 Clean Code Part1
    小课堂Week11 会说话的代码
    小课堂Week10 例外处理设计的逆袭Part3
    Spark菜鸟学习营Day6 分布式代码运行调试
    UML(一) 类图及类间关系
    分布式事务(一)两阶段提交及JTA
    Java线程间通信方式剖析——Java进阶(四)
    Java进阶(三)多线程开发关键技术
  • 原文地址:https://www.cnblogs.com/xuanlu/p/12571964.html
Copyright © 2011-2022 走看看