馬踏棋盤演算法
阿新 • • 發佈:2021-08-09
馬踏棋盤演算法介紹
馬踏棋盤演算法也被稱為騎士周遊問題
將馬隨機放在國際象棋的8×8棋盤Board[0~7][0~7]的某個方格中,馬按走棋規則(馬走日字)進行移動。要求每個方格只進入一次,走遍棋盤上全部64個方格
馬踏棋盤演算法分析
馬踏棋盤問題(騎士周遊問題)實際上是圖的深度優先搜尋(DFS)的應用。
如果使用回溯(就是深度優先搜尋)來解決,假如馬兒踏了53個點,如圖:走到了第53個,座標(1,0),發現已經走到盡頭,沒辦法,那就只能回退了,檢視其他的路徑,就在棋盤上不停的回溯…… ,思路分析+程式碼實現
分析第一種方式的問題,並使用貪心演算法(greedyalgorithm)進行優化。解決馬踏棋盤問題.
分析第一種方式的問題,並使用貪心演算法(greedyalgorithm)進行優化。解決馬踏棋盤問題.
程式碼實現
import java.awt.Point; import java.util.ArrayList; import java.util.Comparator; public class HorseChessboard { private static int X; // 棋盤的列數 private static int Y; // 棋盤的行數 //建立一個數組,標記棋盤的各個位置是否被訪問過 private static boolean visited[]; //使用一個屬性,標記是否棋盤的所有位置都被訪問 private static boolean finished; // 如果為true,表示成功 public static void main(String[] args) { System.out.println("騎士周遊演算法,開始執行~~"); //測試騎士周遊演算法是否正確 X = 8; Y = 8; int row = 1; //馬兒初始位置的行,從1開始編號 int column = 1; //馬兒初始位置的列,從1開始編號 //建立棋盤 int[][] chessboard = new int[X][Y]; visited = new boolean[X * Y];//初始值都是false //測試一下耗時 long start = System.currentTimeMillis(); traversalChessboard(chessboard, row - 1, column - 1, 1); long end = System.currentTimeMillis(); System.out.println("共耗時: " + (end - start) + " 毫秒"); //輸出棋盤的最後情況 for(int[] rows : chessboard) { for(int step: rows) { System.out.print(step + "\t"); } System.out.println(); } } /** * 完成騎士周遊問題的演算法 * @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; //row = 4 X = 8 column = 4 = 4 * 8 + 4 = 36 visited[row * X + column] = true; //標記該位置已經訪問 //獲取當前位置可以走的下一個位置的集合 ArrayList<Point> ps = next(new Point(column, row)); //對ps進行排序,排序的規則就是對ps的所有的Point物件的下一步的位置的數目,進行非遞減排序 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 和應該走的步數比較 , //如果沒有達到數量,則表示沒有完成任務,將整個棋盤置0 //說明: step < X * Y 成立的情況有兩種 //1. 棋盤到目前位置,仍然沒有走完 //2. 棋盤處於一個回溯過程 if(step < X * Y && !finished ) { chessboard[row][column] = 0; visited[row * X + column] = false; } else { finished = true; } } /** * 功能: 根據當前位置(Point物件),計算馬兒還能走哪些位置(Point),並放入到一個集合中(ArrayList), 最多有8個位置 * @param curPoint * @return */ public static ArrayList<Point> next(Point curPoint) { //建立一個ArrayList ArrayList<Point> ps = new ArrayList<Point>(); //建立一個Point Point p1 = new Point(); //表示馬兒可以走5這個位置 if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) { ps.add(new Point(p1)); } //判斷馬兒可以走6這個位置 if((p1.x = curPoint.x - 1) >=0 && (p1.y=curPoint.y-2)>=0) { ps.add(new Point(p1)); } //判斷馬兒可以走7這個位置 if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y - 2) >= 0) { ps.add(new Point(p1)); } //判斷馬兒可以走0這個位置 if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y - 1) >= 0) { ps.add(new Point(p1)); } //判斷馬兒可以走1這個位置 if ((p1.x = curPoint.x + 2) < X && (p1.y = curPoint.y + 1) < Y) { ps.add(new Point(p1)); } //判斷馬兒可以走2這個位置 if ((p1.x = curPoint.x + 1) < X && (p1.y = curPoint.y + 2) < Y) { ps.add(new Point(p1)); } //判斷馬兒可以走3這個位置 if ((p1.x = curPoint.x - 1) >= 0 && (p1.y = curPoint.y + 2) < Y) { ps.add(new Point(p1)); } //判斷馬兒可以走4這個位置 if ((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y + 1) < Y) { ps.add(new Point(p1)); } return ps; } //根據當前這個一步的所有的下一步的選擇位置,進行非遞減排序, 減少回溯的次數 public static void sort(ArrayList<Point> ps) { ps.sort(new Comparator<Point>() { @Override public int compare(Point o1, Point o2) { // TODO Auto-generated method stub //獲取到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; } } }); } }