算法與數據結構學習(59)-程序員常用10種算法(馬踏棋盤算法)

馬踏棋盤算法介紹

馬踏棋盤算法也被稱爲騎士周遊問題
將馬隨機放在國際象棋的8×8棋盤Board[0~7][0~7]的某個方格中,馬按走棋規則(馬走日字)進行移動。要求每個方格只進入一次,走遍棋盤上全部64個方格
在這裏插入圖片描述

騎士周遊(馬踏棋盤)問題的解決步驟和思路

  1. 創建棋盤 chessBoard , 是一個二維數組
  2. 將當前位置設置爲已經訪問,然後根據當前位置,計算馬兒還能走哪些位置,並放入到一個集合中(ArrayList), 最多有8個位置, 每走一步,就使用step+1
  3. 遍歷ArrayList中存放的所有位置,看看哪個可以走通 , 如果走通,就繼續,走不通,就回溯.
  4. 判斷馬兒是否完成了任務,使用 step 和應該走的步數比較 , 如果沒有達到數量,則表示沒有完成任務,將整個棋盤置0

注意:馬兒不同的走法(策略),會得到不同的結果,效率也會有影響(優化)

//創建一個Point
Point p1 = new Point();
if((p1.x = curPoint.x - 2) >= 0 && (p1.y = curPoint.y -1) >= 0) {
ps.add(new Point(p1));
}

使用貪心算法對原來的算法優化

  1. 我們獲取當前位置,可以走的下一個位置的集合
    //獲取當前位置可以走的下一個位置的集合
    ArrayList ps = next(new Point(column, row));
  2. 我們需要對 ps 中所有的Point 的下一步的所有集合的數目,進行非遞減排序,就ok ,
    9, 7, 6, 5, 3, 2 , 1 //遞減排序
    1, 2, 3, 4,5,6, 10, //遞增排序
    1, 2, 2, 2, 3,3, 4, 5, 6 // 非遞減
    9, 7, 6,6, 6, 5,5, 3, 2 , 1 //非遞增

代碼實現

package horse;

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;
				}
			}
			
		});
	}
}

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章