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:


Solution:

class Solution {
    public int slidingPuzzle(int[][] board) {
        Map<Integer, List<Integer>> map = new HashMap<>();
        // 0 1 2
        // 3 4 5
        map.put(0, Arrays.asList(1, 3));
        map.put(1, Arrays.asList(0, 2, 4));
        map.put(2, Arrays.asList(1, 5));
        map.put(3, Arrays.asList(0, 4));
        map.put(4, Arrays.asList(3, 5, 1));
        map.put(5, Arrays.asList(4, 2));
        Deque<String> queue = new ArrayDeque<>();
        Set<String> visited = new HashSet<>();
        String state = calculateState(board);
        queue.offer(state);
        visited.add(state);
        int step = -1;
        while (!queue.isEmpty()) { 
            int size = queue.size();
            step ++;
            for (int i = 0; i < size; i ++) {
                String curState = queue.poll();
                if (curState.equals("123450")) {
                    return step;
                }
                int zero = zeroIndex(curState);
                for (int n : map.get(zero)) {
                    String nextState = swap(zero, n, curState);
                    if (visited.add(nextState)) {
                        queue.offer(nextState);
                    }
                }
            }
        }
        return -1;
    }
    
    private String swap(int x, int y, String state) {
        char[] arr = state.toCharArray();
        char temp = arr[x];
        arr[x] = arr[y];
        arr[y] = temp;
        return new String(arr);
    }    
    
    private int zeroIndex(String state) {
        return state.indexOf("0");
    }
    
    private String calculateState(int[][] board) {
        int state = 0;
        for (int i = 0; i < 2; i ++) {
            for (int j = 0; j < 3; j ++) {
                int p = 5 - (i * 3 + j);
                state += (int) Math.pow(10, p) * board[i][j];
            }
        }
        if (state < 100000) { 
            return "0" + String.valueOf(state);
        }
        return String.valueOf(state);
    }
}