★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/)
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-dacaclrj-me.html
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
On a 2x3 board
, there are 5 tiles represented by the integers 1 through 5, and an empty square represented by 0.git
A move consists of choosing 0
and a 4-directionally adjacent number and swapping it.github
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:app
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:ide
board
will be a 2 x 3 array as described above.board[i][j]
will be a permutation of [0, 1, 2, 3, 4, 5]
.在一個 2 x 3 的板上(board
)有 5 塊磚瓦,用數字 1~5
來表示, 以及一塊空缺用 0
來表示.ui
一次移動定義爲選擇 0
與一個相鄰的數字(上下左右)進行交換.spa
最終當板 board
的結果是 [[1,2,3],[4,5,0]]
謎板被解開。code
給出一個謎板的初始狀態,返回最少能夠經過多少次移動解開謎板,若是不能解開謎板,則返回 -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]
的排列.1 class Solution { 2 func slidingPuzzle(_ board: [[Int]]) -> Int { 3 var res:Int = 0 4 var visited:Set<[[Int]]> = Set<[[Int]]>() 5 var q:[([[Int]],[Int])] = [([[Int]],[Int])]() 6 var correct:[[Int]] = [[1, 2, 3],[4, 5, 0]] 7 var dirs:[[Int]] = [[0, -1],[-1, 0],[0, 1],[1, 0]] 8 for i in 0..<2 9 { 10 for j in 0..<3 11 { 12 if board[i][j] == 0 13 { 14 q.append((board, [i, j])) 15 } 16 } 17 } 18 while(!q.isEmpty) 19 { 20 for i in stride(from:q.count - 1,through:0,by:-1) 21 { 22 var t:[[Int]] = q.first!.0 23 var zero:[Int] = q.first!.1 24 q.removeFirst() 25 if t == correct 26 { 27 return res 28 } 29 visited.insert(t) 30 for dir in dirs 31 { 32 var x:Int = zero[0] + dir[0] 33 var y:Int = zero[1] + dir[1] 34 if x < 0 || x >= 2 || y < 0 || y >= 3 35 { 36 continue 37 } 38 var cand:[[Int]] = t 39 var temp:Int = cand[zero[0]][zero[1]] 40 cand[zero[0]][zero[1]] = cand[x][y] 41 cand[x][y] = temp 42 if visited.contains(cand) {continue} 43 q.append((cand,[x,y])) 44 } 45 } 46 res += 1 47 } 48 return -1 49 } 50 }