leetcode每日一題系列-滑動謎題

leetcode-773-滑動謎題

這是我參與更文挑戰的第4天,活動詳情查看: 更文挑戰html

[博客連接]

一個菜🐔的學習之路java

掘金首頁git

[題目描述]

在一個 2 x 3 的板上(board)有 5 塊磚瓦,用數字 1~5 來表示, 以及一塊空缺用 0 來表示. 

 一次移動定義爲選擇 0 與一個相鄰的數字(上下左右)進行交換. 

 最終當板 board 的結果是 [[1,2,3],[4,5,0]] 謎板被解開。 

 給出一個謎板的初始狀態,返回最少能夠經過多少次移動解開謎板,若是不能解開謎板,則返回 -1 。 

 示例: 


輸入:board = [[1,2,3],[4,0,5]]
輸出:1
解釋:交換 0 和 5 ,1 步完成



輸入:board = [[1,2,3],[5,4,0]]
輸出:-1
解釋:沒有辦法完成謎板



輸入:board = [[4,1,2],[5,0,3]]
輸出:5
解釋:
最少完成謎板的最少移動次數是 5 ,
一種移動路徑:
還沒有移動: [[4,1,2],[5,0,3]]
移動 1 次: [[4,1,2],[0,5,3]]
移動 2 次: [[0,1,2],[4,5,3]]
移動 3 次: [[1,0,2],[4,5,3]]
移動 4 次: [[1,2,0],[4,5,3]]
移動 5 次: [[1,2,3],[4,5,0]]



輸入:board = [[3,2,4],[1,5,0]]
輸出:14


 提示: 


 board 是一個如上所述的 2 x 3 的數組. 
 board[i][j] 是一個 [0, 1, 2, 3, 4, 5] 的排列. 

 Related Topics 廣度優先搜索 數組 矩陣 
 👍 154 👎 0
複製代碼

[題目連接]

leetcode題目連接github

[github地址]

代碼連接數組

[思路介紹]

思路一:BFS+二維壓縮一維markdown

  • 思路分析,這個題有點相似小時候玩的智能拼圖?我記得是叫這個名字,只不過這個是固定了起點
  • 首先找到起點座標,而後移動起點座標
  • 二維座標轉換爲一維座標index = x*3+y
  • 題解target也就能夠轉換爲「1,2,3,4,5,0」
  • 起點一共能移動四個方向(1,0)(0,1)(-1,0)(0,-1)
  • 移動須要保證不能越界
  • 經過廣度優先遍歷移動並交換起點元素
  • 打印移動後字符串,知足target解答便可
//存儲交換節點後字符串以及0的座標
        class Node {
            String s;
            int x,  y;

            public Node(String s, int x, int y) {
                this.s = s;
                this.x = x;
                this.y = y;
            }

            public Node() {
            }
        }
        public int slidingPuzzle(int[][] board) {
            String target = "123450";
            String s = "";
            int sx = 0, sy = 0;
            Node root =new Node();
            int m = board.length, n = board[0].length;
            for (int i = 0; i < m; i++) {
                for (int j = 0; j < n; j++) {
                    s += board[i][j];
                    //遍歷確認起始座標
                    if (0 == board[i][j]) {
                        sx = i;
                        sy = j;
                    }
                    root = new Node(s,sx,sy);
                }
            }
            //方向向量
            int[][] dirs = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
            Deque<Node> deque = new LinkedList<>();
            Map<String , Integer> map = new HashMap<>();
            deque.add(root);
            map.put(s, 0);
            while (!deque.isEmpty()) {
                Node tempNode = deque.poll();
                String str = tempNode.s;
                sx = tempNode.x;
                sy = tempNode.y;
                if (str.equals(target)) {
                    return map.get(str);
                }
                for (int[] dir : dirs
                ) {
                    char[] ctr = str.toCharArray();
                    int nx = sx + dir[0], ny = sy + dir[1];
                    if (nx < 0 || nx >= m || ny < 0 || ny >= n) {
                        continue;
                    }
                    //交換數組
                    char tmp = ctr[sx * 3 + sy];
                    ctr[sx * 3 + sy] = ctr[nx * 3 + ny];
                    ctr[nx * 3 + ny] = tmp;
                    String temStr = String.valueOf(ctr, 0, 6);
                    if (temStr.equals(target)) {
                        return map.get(str) + 1;
                    }
                    if (!map.containsKey(temStr)) {
                        deque.add(new Node(temStr,nx,ny));
                        map.put(temStr, map.get(str) + 1);
                    }
                }
            }
            return -1;
        }
複製代碼

時間複雜度O( ( m n ) ! m n (mn)!*mn )app

相關文章
相關標籤/搜索