LeetCode 773

原题Sliding Puzzle

class Solution{
        Map resMap = new HashMap();
        int[][] steps = {{0, 1}, {0, - 1},{1, 0}, {-1, 0}};
        class State{
            int[][] state;
            int x;
            int y;
            int step_count;

            public State(int[][] state, int x, int y, int step_count) {
                this.state = state;
                this.x = x;
                this.y = y;
                this.step_count = step_count;
            }
        }

        public int slidingPuzzle(int[][] board) {
            int x_p = 0;
            int y_p = 0;

            for (int i = 0; i < 2; i++){
                for (int j = 0; j < 3; j++){
                    if(board[i][j] == 0){
                        x_p = i;
                        y_p = j;
                    }
                }
            }
            bfs(board, x_p, y_p);
            return resMap.getOrDefault("123450", -1);
        }

        private void bfs(int[][] board, int x_p, int y_p) {
            
            if (board2String(board).equals("123450")){
                resMap.put("123450", 0);
                return;
            }
            
            Queue queue = new LinkedList<>();
            queue.offer(new State(board.clone(),x_p, y_p, 0));
            while (!queue.isEmpty()){
                State s = queue.poll();
                x_p = s.x;
                y_p = s.y;
                int count = s.step_count + 1;
                //每次走一步
                for (int[] step:
                        steps){
                    board = new int[2][3];

                    for (int i = 0; i < 2; i++)
                        for(int j = 0; j < 3; j++)
                            board[i][j] = s.state[i][j];
                        // System.arraycopy(s.state[i], 0, board[i], 0, 3);


                    if (x_p + step[0] < 0 || x_p + step[0] > 1
                            ||y_p + step[1] < 0 || y_p + step[1] > 2){
                        continue;
                    }
                    swap(board,x_p,y_p,x_p + step[0], y_p + step[1]);
                    String tmp_state = board2String(board);

                    if (resMap.get(tmp_state) == null || resMap.get(tmp_state) > count){
                        resMap.put(tmp_state, count);
                        queue.offer(new State(board,x_p + step[0], y_p + step[1],count));
                    }
                }
            }

        }

        private void swap(int[][] board, int x_p, int y_p, int des_x, int des_y){
            int t = board[x_p][y_p];
            board[x_p][y_p] = board[des_x][des_y];
            board[des_x][des_y] = t;
        }

        private String board2String(int[][] board){
            StringBuilder res = new StringBuilder();
            for (int i = 0; i < 2; i++){
                for (int j = 0; j < 3; j++){
                    res.append(board[i][j]);
                }
            }
            return res.toString();
        }
    
    }

What I want to note:

一开始打算用DFS,发现每次DFS前后都要维护状态,且不符合题意。题意是说最少的步数DFS适合最大值以及有明确边界条件的问题或者换一种说法DFS是一种和最大以及回溯剪枝有关的解法;而BFS确是和最小有关可无边界最小值问题。

你可能感兴趣的:(LeetCode 773)