一、介紹
- 馬踏棋盤算法也被稱爲騎士周遊問題
- 將馬隨機放在國際象棋的8×8棋盤Board[0~7][0~7]的某個方格中,馬按走棋規則(馬走日字)進行移動。要求每個方格只進入一次,走遍棋盤上全部64個方格
- 遊戲演示
二、思路分析
使用回溯(就是深度優先搜索)來解決。
- 創建棋盤 chessBoard , 是一個二維數組
- 將當前位置設置爲已經訪問,然後根據當前位置,計算馬兒還能走哪些位置,並放入到一個集合中(ArrayList), 最多有8個位置, 每走一步,就使用step+1
- 遍歷ArrayList中存放的所有位置,看看哪個可以走通 , 如果走通,就繼續,走不通,就回溯.
- 判斷馬兒是否完成了任務,使用 step 和應該走的步數比較 , 如果沒有達到數量,則表示沒有完成任務,將整個棋盤置0
可使用貪心算法優化
- 我們獲取當前位置,可以走的下一個位置的集合
- 我們需要對 ps 中所有的Point 的下一步的所有集合的數目,進行非遞減排序,就ok。
注意:馬兒不同的走法(策略),會得到不同的結果,效率也會有影響(優化)
三、代碼實現
import java.awt.*;
import java.util.ArrayList;
public class HouseChessBoard {
//棋盤的列
private static int X;
//棋盤的行
private static int Y;
//創建一個數組,標記棋盤的各個位置是否被訪問過
private static boolean visited[];
//使用一個屬性,標記是否期盼的所有位置都被訪問
private static boolean isFinished;
public static void main(String[] args) {
System.out.println("騎士周遊 - start");
//行與列
X = 6;
Y = 6;
//馬的起始位置,從1開始
int row = 3;
int column = 3;
//創建棋盤
int[][] chessBoard = new int[X][Y];
//初始值爲false;
visited = new boolean[X * Y];
//測試耗時
long begin = System.currentTimeMillis();
//進行騎士周遊運算
traversalChessBoard(chessBoard, row - 1, column - 1, 1);
long end = System.currentTimeMillis();
System.out.println("耗時:" + (end - begin));
System.out.println("騎士周遊 - end");
show(chessBoard);
}
public static ArrayList<Point> next(Point curPoint) {
//創建一個集合存儲點
ArrayList<Point> ps = new ArrayList<>();
//創建一個point
Point p1 = new Point();
//表示馬可以走 左上偏左 的位置
if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
//表示馬可以走 左上偏右 的位置
if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
//表示馬可以走 右上偏左 的位置
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) {
ps.add(new Point(p1));
}
//表示馬可以走 右上偏右 的位置
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) {
ps.add(new Point(p1));
}
//表示馬可以走 右下偏右 的位置
if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
//表示馬可以走 右下偏左 的位置
if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//表示馬可以走 左下偏右 的位置
if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) {
ps.add(new Point(p1));
}
//表示馬可以走 左下偏左 的位置
if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) {
ps.add(new Point(p1));
}
return ps;
}
/**
* 根據當前這一步的所有的下一步的選擇位置,進行非遞減排序,減少回溯次數
*
* @param ps
*/
public static void sort(ArrayList<Point> ps) {
ps.sort(
(o1, o2) -> {
//獲取到o1的下一步的所有位置個數
int count1 = next(o1).size();
//獲取到o2的下一步的所有位置個數
int count2 = next(o2).size();
if (count1 < count2) {
return -1;
} else if (count1 == count2) {
return 0;
} else {
return 1;
}
}
);
}
/**
* 騎士周遊算法
*
* @param chessboard 棋盤
* @param row 馬的當前行 從0開始
* @param column 馬的當前列 從0開始
* @param step 第幾步,從1開始
*/
public static void traversalChessBoard(int[][] chessboard, int row, int column, int step) {
//獲取當前位置
chessboard[row][column] = step;
//標記當前位置爲已訪問
visited[row * X + column] = true;
//獲取當前位置可以走的下一個位置的集合
ArrayList<Point> ps = next(new Point(column, row));
//貪心算法優化,對ps進行優化排序
sort(ps);
//開始遍歷ps
while (!ps.isEmpty()) {
//取出下一個可以移動的位置
Point p = ps.remove(0);
//判斷當前點是否已經訪問過
if (!visited[p.y * X + p.x]) {
//說明沒有訪問過
traversalChessBoard(chessboard, p.y, p.x, step + 1);
}
}
//判斷馬是否走完了所有位置,使用step和應走的步數比較
if (step < X * Y && !isFinished) {
chessboard[row][column] = 0;
visited[row * X + column] = false;
} else {
isFinished = true;
}
}
public static void show(int[][] chessBoard) {
for (int[] rows : chessBoard) {
for (int step : rows) {
System.out.print(step + "\t");
}
System.out.println();
}
}
}