首页 > 其他 > 详细

[LintCode] 794. Sliding Puzzle II

时间:2020-03-26 09:51:36      阅读:64      评论:0      收藏:0      [点我收藏+]

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;
    }
}

 

[LintCode] 794. Sliding Puzzle II

原文:https://www.cnblogs.com/xuanlu/p/12571964.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!