@author: sdubrz
@date: 2020.05.26
題號: 773
題目難度: 困難
考察內容: BFS
原題鏈接 https://leetcode-cn.com/problems/sliding-puzzle/
題目的著作權歸領釦網絡所有,商業轉載請聯繫官方授權,非商業轉載請註明出處。
解題代碼轉載請聯繫 lwyz521604#163.com
在一個 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] 的排列.
通過次數2,667 提交次數4,553
BFS解法
一般這種狀態變換的最小次數可以用BFS來暴力搜索解決,示意圖如下。
爲了提高效率,可以用一個 HashSet 來判斷某一個狀態是否到達過,如果達到過直接剪枝。
具體的實現代碼如下:
import java.util.*;
class Solution {
public int slidingPuzzle(int[][] board) {
int[][] target = {
{1, 2, 3},
{4, 5, 0}
};
int count = 0;
if(finished(board, target)) {
return count;
}
int n = board.length;
int m = board[0].length;
Queue<int[][]> queue1 = new LinkedList<>();
Queue<Integer> xQueue1 = new LinkedList<>(); // 記錄0元素的位置
Queue<Integer> yQueue1 = new LinkedList<>(); //
HashSet<String> set = new HashSet<>();
queue1.add(board);
set.add(arrayString(board));
int[] location = find0(board);
xQueue1.add(location[0]);
yQueue1.add(location[1]);
while(!queue1.isEmpty()) {
count++;
Queue<int[][]> queue2 = new LinkedList<>();
while(!queue1.isEmpty()) {
int[][] root = queue1.poll();
int x = xQueue1.poll();
int y = yQueue1.poll();
if(x>0) { // 和左邊的交換
int[][] leef = new int[n][m];
for(int i=0; i<n; i++) {
leef[i] = root[i].clone();
}
leef[x][y] = root[x-1][y];
leef[x-1][y] = root[x][y];
String str = arrayString(leef);
if(set.add(str)) {
if(finished(leef, target)) {
return count;
}
queue2.add(leef);
xQueue1.add(x-1);
yQueue1.add(y);
}
}
if(x<n-1) { // 和右邊的交換
int[][] leef = new int[n][m];
for(int i=0; i<n; i++) {
leef[i] = root[i].clone();
}
leef[x][y] = root[x+1][y];
leef[x+1][y] = root[x][y];
String str = arrayString(leef);
if(set.add(str)) {
if(finished(leef, target)) {
return count;
}
queue2.add(leef);
xQueue1.add(x+1);
yQueue1.add(y);
}
}
if(y>0) { // 和上面的交換
int[][] leef = new int[n][m];
for(int i=0; i<n; i++) {
leef[i] = root[i].clone();
}
leef[x][y] = root[x][y-1];
leef[x][y-1] = root[x][y];
String str = arrayString(leef);
if(set.add(str)) {
if(finished(leef, target)) {
return count;
}
queue2.add(leef);
xQueue1.add(x);
yQueue1.add(y-1);
}
}
if(y<m-1) { // 和下面的交換
int[][] leef = new int[n][m];
for(int i=0; i<n; i++) {
leef[i] = root[i].clone();
}
leef[x][y] = root[x][y+1];
leef[x][y+1] = root[x][y];
String str = arrayString(leef);
if(set.add(str)) {
if(finished(leef, target)) {
return count;
}
queue2.add(leef);
xQueue1.add(x);
yQueue1.add(y+1);
}
}
}
queue1 = queue2;
}
return -1;
}
// 判斷是否是最終的結果
private boolean finished(int[][] array, int[][] target) {
for(int i=0; i<target.length; i++) {
for(int j=0; j<target[i].length; j++) {
if(array[i][j]!=target[i][j]) {
return false;
}
}
}
return true;
}
// 將數組轉化爲字符串,方便做Hash
private String arrayString(int[][] array) {
String str = "";
for(int i=0; i<array.length; i++) {
for(int j=0; j<array[i].length; j++) {
str = str + array[i][j];
}
}
return str;
}
// 尋找0元素的位置
private int[] find0(int[][] array) {
int x = -1;
int y = -1;
for(int i=0; i<array.length; i++) {
for(int j=0; j<array[i].length; j++) {
if(array[i][j]==0) {
x = i;
y = j;
break;
}
}
if(x>=0) {
break;
}
}
int[] result = {x, y};
return result;
}
}
在 LeetCode 系統中提交的結果如下所示
執行結果: 通過 顯示詳情
執行用時 : 35 ms, 在所有 Java 提交中擊敗了 13.85% 的用戶
內存消耗 : 40.3 MB, 在所有 Java 提交中擊敗了 100.00% 的用戶
其他方法
這個題其實就是在人工智能課上學過的八數碼問題。可以有多種解決方法,如動態規劃、A* 算法、BFS、DFS等。因爲BFS思路和代碼都比較簡單,所以我先用BFS來嘗試的。其實 A* 算法的效率要優於BFS。有時間會在這裏進行補充。