如何基于java語言實(shí)現(xiàn)八皇后問題
這篇文章主要介紹了如何基于java語言實(shí)現(xiàn)八皇后問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
八皇后問題,在一個(gè)8X8的棋盤中,放置八個(gè)棋子,每個(gè)棋子的上下左右,左上左下,右上右下方向上不得有其他棋子。正確答案為92中,接下來用java語言實(shí)現(xiàn)。
代碼如下
package eightQuen; /** * 八皇后問題 * * @author 83771 * */ public class eight { // 定義一個(gè)數(shù)組 表示棋盤 public static Integer[][] checkerBoard = new Integer[8][8]; // 棋盤副本 public static Integer[][] checkerBoardCopy = new Integer[8][8]; // 計(jì)數(shù)器 用于計(jì)數(shù)有多少種方法 public static Integer jishu = 1; // 定義橫豎斜方向上是否有棋子 public static boolean flag1 = true; public static boolean flag2 = true; public static boolean flag3 = true; public static boolean flag4 = true; // 初始化一個(gè)棋盤 8x8 public static void init() { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.print(0 + " "); checkerBoard[i][j] = 0; } System.out.println(); } checkerBoardCopy = checkerBoard; } // 遞歸測試方法 public static void startTest(int row) { for (int col = 0; col < 8; col++) { if (checkCheet(row, col, checkerBoardCopy) == 1) { if (row < 7) { startTest(++row); --row; } } // 該行重新賦值為0 進(jìn)行下一次判斷 checkerBoardCopy[row][col] = 0; } } // 檢查是否危險(xiǎn) // row行 // col列 public static int checkCheet(int row, int col, Integer[][] checkerBoardCopy) { flag1 = true; flag2 = true; flag3 = true; flag4 = true; // 行方向上是否滿足條件 for (int i = 0; i < 8; i++) { if (checkerBoardCopy[row][i] == 1) { flag1 = false; break; } } // 列方向上是否滿足條件 for (int j = 0; j < 8; j++) { if (checkerBoardCopy[j][col] == 1) { flag2 = false; break; } } // 右下方向 for (int i = row, j = col; i < 8 & j < 8; i++, j++) { if (checkerBoardCopy[i][j] == 1) { flag3 = false; break; } } // 左上方向 for (int i = row, j = col; i >= 0 & j >= 0; i--, j--) { if (checkerBoardCopy[i][j] == 1) { flag3 = false; break; } } // 左下方向 for (int i = row, j = col; i < 8 & j >= 0; i++, j--) { if (checkerBoardCopy[i][j] == 1) { flag4 = false; break; } } // 右上方向 for (int i = row, j = col; i >= 0 & j < 8; i--, j++) { if (checkerBoardCopy[i][j] == 1) { flag4 = false; break; } } if (flag1 & flag2 & flag3 & flag4) { // 若為真 增此點(diǎn)的值賦為1 checkerBoardCopy[row][col] = 1; // 如果已經(jīng)判斷到最后一行 并且最后一行也符合情況 打印整個(gè)棋盤 if (row == 7) { printCheets(checkerBoardCopy); } return 1; } return 0; } // 打印棋盤方法 public static void printCheets(Integer[][] checkerBoardCopy) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { System.out.print(checkerBoardCopy[i][j] + " "); } System.out.println(); } System.out.println("=================" + jishu++); } public static void main(String[] args) { init(); startTest(0); } }
copy后可直接運(yùn)行。 記一下這次的代碼。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java 異步編程實(shí)踐_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
異步編程提供了一個(gè)非阻塞的,事件驅(qū)動(dòng)的編程模型。下面通過本文給大家介紹Java 異步編程實(shí)踐,感興趣的的朋友一起看看吧2017-05-05詳談cxf和axis兩種框架下的webservice客戶端開發(fā)
這篇文章主要介紹了詳談cxf和axis兩種框架下的webservice客戶端開發(fā),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08使用IDEA直接連接MySQL數(shù)據(jù)庫的方法
這篇文章主要介紹了如何使用IDEA直接連接MySQL數(shù)據(jù)庫,首先需要新建一個(gè)空項(xiàng)目,第一次連接 需要先下載驅(qū)動(dòng),文中給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-04-04SpringBoot JWT實(shí)現(xiàn)token登錄刷新功能
JWT本身是無狀態(tài)的,這點(diǎn)有別于傳統(tǒng)的session,不在服務(wù)端存儲憑證。這種特性使其在分布式場景,更便于擴(kuò)展使用。接下來通過本文給大家分享SpringBoot JWT實(shí)現(xiàn)token登錄刷新功能,感興趣的朋友一起看看吧2021-09-09Java輕松使用工具類實(shí)現(xiàn)獲取MP3音頻時(shí)長
在Java中,工具類定義了一組公共方法,這篇文章將介紹Java中使用工具類來獲取一個(gè)MP3音頻文件的時(shí)間長度,感興趣的同學(xué)繼續(xù)往下閱讀吧2021-10-10用java開發(fā)dota英雄最華麗的技能(實(shí)例講解)
下面小編就為大家分享一篇使用java開發(fā)dota英雄最華麗的技能實(shí)例,具有非常好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11