Java基于循环递归回溯实现八皇后问题算法示例

所属分类: 软件编程 / java 阅读数: 37
收藏 0 赞 0 分享

本文实例讲述了Java基于循环递归回溯实现八皇后问题。分享给大家供大家参考,具体如下:

运行效果图如下:

棋盘接口

/**
 * 棋盘接口
 * @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;
  }
  //当前行是否能放棋子
  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;
  }
  //主要的递归实现方法
  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个则打印结果
        }
        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);
  }
}

更多关于java相关内容感兴趣的读者可查看本站专题:《Java数据结构与算法教程》、《Java字符与字符串操作技巧总结》、《java日期与时间操作技巧汇总》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

更多精彩内容其他人还在看

Java concurrency之锁_动力节点Java学院整理

这篇文章主要为大家详细介绍了Java concurrency之锁的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java8新特性之StampedLock_动力节点Java学院整理

本文从synchronized、Lock到Java8新增的StampedLock进行对比分析,对Java8新特性之StampedLock相关知识感兴趣的朋友一起看看吧
收藏 0 赞 0 分享

Java8新特性之lambda的作用_动力节点Java学院整理

我们期待了很久lambda为java带来闭包的概念,但是如果我们不在集合中使用它的话,就损失了很大价值。现有接口迁移成为lambda风格的问题已经通过default methods解决了,在这篇文章将深入解析Java集合里面的批量数据操作解开lambda最强作用的神秘面纱。
收藏 0 赞 0 分享

Java8新特性之Base64详解_动力节点Java学院整理

这篇文章主要为大家详细介绍了Java8新特性之Base64的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java8新特性之JavaFX 8_动力节点Java学院整理

这篇文章主要介绍了Java8新特性之JavaFX 8的相关知识,非常不错,具有参考借鉴价值,需要的朋友参考下吧
收藏 0 赞 0 分享

将本地jar包安装进入maven仓库(实现方法)

下面小编就为大家带来一篇将本地jar包安装进入maven仓库(实现方法)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

浅谈Java finally语句到底是在return之前还是之后执行(必看篇)

下面小编就为大家带来一篇浅谈Java finally语句到底是在return之前还是之后执行(必看篇)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

基于Java并发容器ConcurrentHashMap#put方法解析

下面小编就为大家带来一篇基于Java并发容器ConcurrentHashMap#put方法解析。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

详解Spring Boot Profiles 配置和使用

本篇文章主要介绍了详解Spring Boot Profiles 配置和使用,具有一定的参考价值,有兴趣的可以了解一下
收藏 0 赞 0 分享

详解Spring Boot 属性配置和使用

本篇文章主要介绍了详解Spring Boot 属性配置和使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享
查看更多