Java基于循環(huán)遞歸回溯實(shí)現(xiàn)八皇后問(wèn)題算法示例
本文實(shí)例講述了Java基于循環(huán)遞歸回溯實(shí)現(xiàn)八皇后問(wèn)題。分享給大家供大家參考,具體如下:
運(yùn)行效果圖如下:

棋盤(pán)接口
/**
* 棋盤(pán)接口
* @author Administrator
*
*/
public interface Piece {
abstract boolean isRow(int line);
abstract boolean isCol(int line,int col);
}
棋盤(pán)類(lèi):
/**
* 棋盤(pán)
* @author Administrator
*
*/
public class Chessboard implements Piece {
static boolean[][] che = null;
public int row;
public int col;
private int num=0;
public Chessboard (int row,int col){
this.che=new boolean[row][col];
this.row=row;
this.col=col;
}
//當(dāng)前行是否能放棋子
public boolean isRow(int line){
for (int i = 0; i < this.row; i++) {
if (che[i][line] == true) {
return false;
}
}
return true;
}
//棋子邊角
public boolean isCol(int line,int col){
int i = 0, j = 0;
for (i = line, j = col; i < this.row && j < this.row; i++, j++) { //右下角;
if (che[i][j] == true) {
return false;
}
}
for (i = line, j = col; i >= 0 && j >= 0; i--, j--) { //左上角;
if (che[i][j] == true) {
return false;
}
}
for (i = line, j = col; i >= 0 && j < this.row; i--, j++) { // 右上角;
if (che[i][j] == true) {
return false;
}
}
for (i = line, j = col; i < this.row && j >= 0; i++, j--) { //左下角;
if (che[i][j] == true) {
return false;
}
}
return true;
}
public void pr() {//打印滿(mǎn)足條件的擺放方法
num++;
System.out.println("第" + num + "種方式");
System.out.print("-------------start-------------");
for (int i = 0; i < this.row; i++) {
System.out.println();
for (int j = 0; j < this.row; j++) {
if (che[i][j] == true) {
System.out.print("Q ");
} else {
System.out.print(". ");
}
}
}
System.out.println();
System.out.println("-------------end-------------");
}
}
皇后類(lèi)
/**
* 皇后
* @author Administrator
*
*/
public class empress {
private Chessboard che=null;
private int count=0;
private int row=0;
public empress(int row,int col){
this.che=new Chessboard(row,col);
this.row=row;
}
//主要的遞歸實(shí)現(xiàn)方法
public void mk(int line) {
if (line > this.row-1)
return;//超過(guò)行則退出
for (int i = 0; i < this.row; i++) {
if (che.isRow(i) && che.isCol(line,i)) { //ture 為可以擺皇后;
che.che[line][i] = true; //
count++; //
if (count > this.row-1) {
che.pr();//擺放皇后8個(gè)則打印結(jié)果
}
mk(line + 1);//遞歸
che.che[line][i] = false; //回溯
count--;
continue;
}
}
return;
}
}
啟動(dòng):
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class start {
public static void main(String[] args) {
String inputrow = JOptionPane.showInputDialog("輸入行:");
int row = Integer.parseInt(inputrow);
String inputcol = JOptionPane.showInputDialog("輸入列:");
int col = Integer.parseInt(inputcol);
empress emp=new empress(row,col);
emp.mk(0);
}
}
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《java日期與時(shí)間操作技巧匯總》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Mybatis一對(duì)多查詢(xún)的兩種姿勢(shì)(值得收藏)
這篇文章主要給大家介紹了關(guān)于Mybatis一對(duì)多查詢(xún)的兩種姿勢(shì),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
詳解servlet的url-pattern匹配規(guī)則
本篇文章主要介紹了=servlet的url-pattern匹配規(guī)則,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
spring security獲取用戶(hù)信息為null或者串值的解決
這篇文章主要介紹了spring security獲取用戶(hù)信息為null或者串值的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
java中form以post、get方式提交數(shù)據(jù)中文亂碼問(wèn)題總結(jié)
這篇文章主要介紹了java中form以post、get方式提交數(shù)據(jù)中文亂碼問(wèn)題總結(jié),需要的朋友可以參考下2014-10-10
如何解決org.apache.jasper.JasperException:無(wú)法為JSP編譯類(lèi)詳解
這篇文章主要給大家介紹了關(guān)于如何解決org.apache.jasper.JasperException:無(wú)法為JSP編譯類(lèi)的相關(guān)資料,原因可能是JSP文件的語(yǔ)法錯(cuò)誤、類(lèi)路徑問(wèn)題或其他配置問(wèn)題,建議檢查JSP文件的語(yǔ)法、類(lèi)路徑配置和其他相關(guān)配置,需要的朋友可以參考下2023-06-06
Spring Boot Admin 動(dòng)態(tài)修改日志級(jí)別的方法步驟
這篇文章主要介紹了Spring Boot Admin 動(dòng)態(tài)修改日志級(jí)別的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
源碼解析Spring 數(shù)據(jù)庫(kù)異常抽理知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享了關(guān)于源碼解析Spring 數(shù)據(jù)庫(kù)異常抽理知識(shí)點(diǎn)內(nèi)容,對(duì)此有需要的朋友們學(xué)習(xí)參考下。2019-05-05

