美文网首页
LeetCode 773

LeetCode 773

作者: portability | 来源:发表于2018-08-12 17:38 被阅读0次

原题Sliding Puzzle

class Solution{
        Map<String, Integer> 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<State> 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

    原题Sliding Puzzle What I want to note: 一开始打算用DFS,发现每次DFS前后...

  • 如何用 BFS 算法秒杀各种智力题

    读完本文,你可以去力扣拿下如下题目: 773.滑动谜题[https://leetcode-cn.com/probl...

  • 几道Leetcode题解

    最近在 leetcode 上做了寄到练习题,运气很好,最新的几道 hard 题目都是少量代码可以搞定的。 773....

  • LeetCode 773. 滑动谜题

    1、题目 2、分析 直接套用BFS的算法框架就可以。要注意“邻居”数组的定义方式和遍历方式 3、代码

  • 773

    善良和爱都是免费的 但不是廉价的 你的善良 需要带点锋芒 你的爱 需要带些理智 毕竟不是所有人都配拥有它们

  • 773

    今日继续阴天,薄款羽绒服也穿上了,体感刚刚好。 我刚才看到广州朋友发的朋友圈,人家都赤膊上阵了,哈,我们根本不是在...

  • 773

    11月10日,农历十月初六,晴,周三 还有一会就是双十一,晚上早早忙好。带着本子和纸,对着手机上备忘录,一样样看,...

  • 〔773〕

    2022/1/14 周五 晴2℃ 假期第一天,除了工作上的不愉快和烦躁,其余一切安好。我不去计较,并不代表我好...

  • 10.20《773》

    昨天累着了早上和儿子说今天早上真不爱动弹,儿子来了一句:妈妈你不用做饭了,我不吃了这样你可以歇一歇,那怎么行早上不...

  • 2020.2.27 773

    我特别喜欢的一段话 每个小孩儿,都是一块磨刀石! 当小孩惹你生气的时候,暂时尽量离他远远的。否则极有可能会让你做出...

网友评论

      本文标题:LeetCode 773

      本文链接:https://www.haomeiwen.com/subject/uwosbftx.html