原题链接在这里:https://leetcode.com/problems/sliding-puzzle/description/
题目:
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:
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]
.
题解:
题目说道least number of moves, 应该想到用BFS.
Queue里面放上现在board的状态, 利用新的类Node, 记录board, 0所在位置和查询的深度.
poll时如果出现了target状态,就找到了结果, 返回深度.
BFS用Set来保存出现过的状态, 这里学到用Arrays.deepToString来把matrix变成string来做hash.
Time Complexity: O((m*n)!*m*n). m = board.length, n = board[0].length. board一共有(m*n)! 种可能状态, 也就是queue的可能长度. 处理queue的每个node用时O(m*n).
Space: O((m*n)!).
AC Java:
1 class Solution { 2 public int slidingPuzzle(int[][] board) { 3 int m = board.length; 4 int n = board[0].length; 5 6 int p = 0; 7 int q = 0; 8 search: 9 for(int i = 0; i<m; i++){ 10 for(int j = 0; j<n; j++){ 11 if(board[i][j] == 0){ 12 p = i; 13 q = j; 14 break search; 15 } 16 } 17 } 18 19 int [][] dirs = {{-1,0},{1,0},{0,-1},{0,1}}; 20 int [][] targetMatrix = {{1,2,3},{4,5,0}}; 21 String target = Arrays.deepToString(targetMatrix); 22 23 LinkedList<Node> que = new LinkedList<Node>(); 24 que.add(new Node(board, p, q, 0)); 25 26 HashSet<String> used = new HashSet<String>(); 27 used.add(Arrays.deepToString(board)); 28 29 while(!que.isEmpty()){ 30 Node cur = que.poll(); 31 if(cur.serializedBoard.equals(target)){ 32 return cur.depth; 33 } 34 35 for(int [] dir : dirs){ 36 int row = cur.i+dir[0]; 37 int col = cur.j+dir[1]; 38 if(row<0 || row>=m || col<0 || col>=n){ 39 continue; 40 } 41 42 int [][] newBoard = new int[m][n]; 43 for(int i = 0; i<m; i++){ 44 for(int j = 0; j<n; j++){ 45 newBoard[i][j] = cur.board[i][j]; 46 } 47 } 48 49 newBoard[cur.i][cur.j] = newBoard[row][col]; 50 newBoard[row][col] = 0; 51 52 Node newNode = new Node(newBoard, row, col, cur.depth+1); 53 if(used.contains(newNode.serializedBoard)){ 54 continue; 55 } 56 57 que.add(newNode); 58 used.add(newNode.serializedBoard); 59 } 60 } 61 62 return -1; 63 } 64 } 65 66 class Node{ 67 int [][] board; 68 String serializedBoard; 69 int i; 70 int j; 71 int depth; 72 public Node(int [][] board, int i, int j, int depth){ 73 this.board = board; 74 this.serializedBoard = Arrays.deepToString(board); 75 this.i = i; 76 this.j = j; 77 this.depth = depth; 78 } 79 }