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.

思路:这题不难,就是个BFS,但是triky的地方就是要把board转换成string,然后0,怎么挪动,用下面二维数组;

when zero is in position:0 it can go to cell {1,3}, position:1 it can go {0,2,4} and so on, also when i mean position i'm talking about the index of zero in the string not the input array.

0 1 2

3 4 5

上面是index,那么0在0,可以挪动到{1,3},0在1的位子,可以挪到{0,2,4},以此类推,因为string,所以这些index是可以用来互换的。

class Solution {
    private final String target = "123450";
    public int slidingPuzzle(int[][] board) {
        if(board == null || board.length == 0 || board[0].length == 0) {
            return 0;
        }
        Queue<String> queue = new LinkedList<String>();
        HashSet<String> visited = new HashSet<String>();
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < board.length; i++) {
            for(int j = 0; j < board[0].length; j++) {
                sb.append(board[i][j]);
            }
        }
        queue.offer(sb.toString());
        visited.add(sb.toString());
        
        int step = 0;
        while(!queue.isEmpty()) {
            int size = queue.size();
            for(int i = 0; i < size; i++) {
                String node = queue.poll();
                if(node.equals(target)) {
                    return step;
                }
                for(String neighbor: getNeighbors(node)) {
                    if(!visited.contains(neighbor)) {
                        visited.add(neighbor);
                        queue.offer(neighbor);
                    }
                }
            }
            step++;
        }
        return -1;
    }
    
    private List<String> getNeighbors(String node) {
        List<String> list = new ArrayList<String>();
        int[][] dir = {{1,3}, {0,2,4}, {1,5}, {0,4}, {1,3,5}, {2,4}};
        int index = node.indexOf('0');
        StringBuilder sb;
        for(int i = 0; i < dir[index].length; i++) {
            sb = new StringBuilder(node);
            swap(sb, list, index, dir[index][i]);
        }
        return list;
    }
    
    private void swap(StringBuilder sb, List<String> list,
                     int i, int j) {
       if(i != j) {
           char temp = sb.charAt(i);
           sb.setCharAt(i, sb.charAt(j));
           sb.setCharAt(j, temp);
           list.add(sb.toString());
       }
    }
}
发布了663 篇原创文章 · 获赞 13 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/u013325815/article/details/105040744
今日推荐