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

棋盤接口
/**
* 棋盤接口
* @author Administrator
*
*/
public interface Piece {
abstract boolean isRow(int line);
abstract boolean isCol(int line,int col);
}
棋盤類:
/**
* 棋盤
* @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() {//打印滿足條件的擺放方法
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-------------");
}
}
皇后類
/**
* 皇后
* @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;
}
//主要的遞歸實現(xiàn)方法
public void mk(int line) {
if (line > this.row-1)
return;//超過行則退出
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個則打印結(jié)果
}
mk(line + 1);//遞歸
che.che[line][i] = false; //回溯
count--;
continue;
}
}
return;
}
}
啟動:
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)容感興趣的讀者可查看本站專題:《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java字符與字符串操作技巧總結(jié)》、《java日期與時間操作技巧匯總》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
相關(guān)文章
idea新建mapper.xml文件詳細(xì)步驟如:mybatis-config
這篇文章主要介紹了idea新建xml模板設(shè)置,例如:mybatis-config,本文分步驟通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07
java中的Io(input與output)操作總結(jié)(三)
這一節(jié)我們來講Scanner類和PrintWriter類的用法,感興趣的朋友可以了解下2013-01-01
java基于netty NIO的簡單聊天室的實現(xiàn)
這篇文章主要介紹了java基于netty NIO的簡單聊天室的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringBoot多環(huán)境開發(fā)與日志小結(jié)
這篇文章主要介紹了SpringBoot多環(huán)境開發(fā)與日志,下面給大家說一下如何基于多環(huán)境開發(fā)做配置獨立管理,務(wù)必掌握,需要的朋友可以參考下2022-08-08
java request.getParameter中文亂碼解決方法
今天跟大家分享幾個解決java Web開發(fā)中,request.getParameter()獲取URL中文參數(shù)亂碼的解決辦法,需要的朋友可以參考下2020-02-02
Spring Boot應(yīng)用發(fā)布到Docker的實現(xiàn)
這篇文章主要介紹了Spring Boot應(yīng)用發(fā)布到Docker的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06
通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis)
這篇文章主要介紹了通過IEAD+Maven快速搭建SSM項目的過程(Spring + Spring MVC + Mybatis),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
解決spring cloud gateway 獲取body內(nèi)容并修改的問題
這篇文章主要介紹了解決spring cloud gateway 獲取body內(nèi)容并修改的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12

