java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤
本文實(shí)例為大家分享了java數(shù)據(jù)結(jié)構(gòu)與算法之馬踏棋盤的具體代碼,供大家參考,具體內(nèi)容如下
- 馬踏棋盤算法也被稱為騎士周游問(wèn)題
- 將馬隨機(jī)放在過(guò)期象棋的8x8棋盤的某個(gè)方格中,馬按走棋規(guī)則進(jìn)行移動(dòng),要求每個(gè)方格只進(jìn)入一次,走遍棋盤上全部64個(gè)方格
騎士周游問(wèn)題結(jié)局步驟和思路
1.創(chuàng)建棋盤chessBoard,是一個(gè)二維數(shù)組
2.將當(dāng)前位置設(shè)置為已個(gè)訪問(wèn),然后根據(jù)當(dāng)前位置,計(jì)算馬兒還能走那些位置,并放到一個(gè)集合中(ArrayList),最多8個(gè)位置
3.變量ArrayList存放的所有位置,看看哪個(gè)可以走通
4.判斷馬兒是否完成了騎士周游問(wèn)題
注意:馬兒不同的走法,會(huì)得到不同的結(jié)果,效率也會(huì)有影響
代碼實(shí)現(xiàn)
public class HorseChessBoard {
?? ?private static int X; ?//棋盤的列數(shù)
?? ?private static int Y; ?//棋盤的行數(shù)
?? ?
?? ?//創(chuàng)建數(shù)組標(biāo)記棋盤各個(gè)位置是否被訪問(wèn)過(guò)
?? ?private static boolean[] visited;
?? ?//使用一個(gè)屬性標(biāo)記是否棋盤的所有位置都被訪問(wèn)過(guò),即是否成功
?? ?private static boolean finish; ?//如果為true表示成功
?? ?
?? ?public static void main(String[] args) {
?? ??? ?X = 8;
?? ??? ?Y = 8;
?? ??? ?int row = 1;
?? ??? ?int col = 1;?
?? ??? ?int[][] chessboard = ?new int[X][Y];
?? ??? ?visited = new boolean[X * Y];
?? ??? ?
?? ??? ?long start = System.currentTimeMillis();
?? ??? ?traversalChessboard(chessboard, row-1, col-1, 1);
?? ??? ?long end = System.currentTimeMillis();
?? ??? ?System.out.println(end - start);
?? ??? ?
?? ??? ?for (int[] rows : chessboard) {
?? ??? ??? ?for (int step : rows) {
?? ??? ??? ??? ?System.out.print(step + " ?");
?? ??? ??? ?}
?? ??? ??? ?System.out.println();
?? ??? ?}
?? ?}
?? ?
?? ?//其實(shí)周游問(wèn)題
?? ?public static void traversalChessboard(int[][] chessboard, int row, int col, int step) {
?? ??? ?
?? ??? ?if (finish) return;
?? ??? ?chessboard[row][col] = step;
?? ??? ?visited[row * X + col] = true; ?//標(biāo)記該位置已經(jīng)訪問(wèn)
?? ??? ?//獲取當(dāng)前位置可以走的下一個(gè)位置的集合
?? ??? ?List<Point> ps = next(new Point(col, row));
?? ??? ?sort(ps);
?? ??? ?
?? ??? ?//遍歷ps
?? ??? ?while (!ps.isEmpty()) {
?? ??? ??? ?Point p = ps.remove(0); ?//取出下一個(gè)可以走的位置
?? ??? ??? ?//判斷該點(diǎn)是否已經(jīng)訪問(wèn)過(guò)
?? ??? ??? ?if (!visited[p.y * X + p.x]) {
?? ??? ??? ??? ?traversalChessboard(chessboard, p.y, p.x, step+1);
?? ??? ??? ?}
?? ??? ?}
?? ??? ?
?? ??? ?//1. 棋盤到目前位置任然未走完
?? ??? ?//2. 棋盤處于一個(gè)回溯過(guò)程
?? ??? ?if (step < X * Y && !finish) {
?? ??? ??? ?chessboard[row][col] = 0;
?? ??? ??? ?visited[row * X + col] = false;
?? ??? ?} else {
?? ??? ??? ?finish = true;
?? ??? ?}
?? ?}
?? ?
?? ?//根據(jù)當(dāng)前這一步的所有的下一步的選擇位置進(jìn)行非遞減排序
?? ?public static void sort(List<Point> ps) {
?? ??? ?ps.sort(new Comparator<Point>() {
?? ??? ??? ?@Override
?? ??? ??? ?public int compare(Point o1, Point o2) {
?? ??? ??? ??? ?//獲取o1,o2下一步所有個(gè)數(shù)
?? ??? ??? ??? ?int count1 = next(o1).size();
?? ??? ??? ??? ?int count2 = next(o2).size();
?? ??? ??? ??? ?if (count1 < count2) {
?? ??? ??? ??? ??? ?return -1;
?? ??? ??? ??? ?} else if (count1 == count2) {
?? ??? ??? ??? ??? ?return 0;
?? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ?return 1;
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?});
?? ?}
?? ?
?? ?//Point:根據(jù)當(dāng)前位置(point對(duì)象)
?? ?//根據(jù)當(dāng)前位置,計(jì)算馬兒還能走那些位置,并放到一個(gè)集合中(ArrayList),最多8個(gè)位置
?? ?public static List<Point> next(Point curPoint) {
?? ??? ?//創(chuàng)建list集合
?? ??? ?List<Point> ps = new ArrayList<>();
?? ??? ?//創(chuàng)建一個(gè)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;
?? ?}
}以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot請(qǐng)求處理之常用參數(shù)注解介紹與源碼分析
SpringBoot是一種整合Spring技術(shù)棧的方式(或者說(shuō)是框架),同時(shí)也是簡(jiǎn)化Spring的一種快速開發(fā)的腳手架,本篇讓我們一起學(xué)習(xí)請(qǐng)求處理、常用注解和方法參數(shù)的小技巧2022-10-10
java中如何對(duì)arrayList按數(shù)字大小逆序排序
這篇文章主要介紹了java中如何對(duì)arrayList按數(shù)字大小逆序排序問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
mybatis實(shí)現(xiàn)動(dòng)態(tài)升降序的問(wèn)題小結(jié)
文章介紹了如何在MyBatis的XML文件中實(shí)現(xiàn)動(dòng)態(tài)排序,使用$符號(hào)而不是#符號(hào)來(lái)引用變量,以避免SQL注入,同時(shí),強(qiáng)調(diào)了在Java代碼中進(jìn)行防注入處理的重要性,感興趣的朋友一起看看吧2025-02-02
Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲
這篇文章主要為大家詳細(xì)介紹了Java+JFrame實(shí)現(xiàn)貪吃蛇小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06
2024年最新IntelliJ?IDEA常用的小技巧總結(jié)(JAVA新手上路必備)
這篇文章主要介紹了2024年最新IntelliJ?IDEA常用小技巧的相關(guān)資料,文中包括IntelliJ?IDEA的概述、下載與安裝、快速創(chuàng)建并運(yùn)行Java工程、詳細(xì)設(shè)置、快速開發(fā)、多模塊的IDEA工程以及最新變化,需要的朋友可以參考下2025-01-01
SpringBoot深入刨析數(shù)據(jù)層技術(shù)
這篇文章主要介紹了SpringBoot數(shù)據(jù)層技術(shù)的解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08
struts2實(shí)現(xiàn)文件上傳顯示進(jìn)度條效果
這篇文章主要為大家詳細(xì)介紹了struts2實(shí)現(xiàn)文件上傳顯示進(jìn)度條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

