循环方式:
package EightQueens;
public class EightQueensNotRecursive {
private static final boolean AVAILABLE = true;
private int squares = 8, norm = squares - 1;
private int positionInRow[] = new int[squares];
private int p=-1;
private boolean[] rows = new boolean[squares];
private boolean[] column = new boolean[squares];
private boolean[] leftDiagonal = new boolean[2 * squares - 1];
private boolean[] rightDiagonal = new boolean[2 * squares - 1];
private static int howMany = 0;
public EightQueensNotRecursive() {
// To complete the initialization work for the
// column,leftDiagonal,rigthDiagonal.
for (int i = 0; i < squares; i++) {
rows[i] = AVAILABLE;
column[i] = AVAILABLE;
positionInRow[i] = -1;
}
for (int i = 0; i < 2 * squares - 1; i++) {
leftDiagonal[i] = AVAILABLE;
rightDiagonal[i] = AVAILABLE;
}
}
public void printResults(int[] columns) {
int row, col;
System.out.println("八皇后问题的第 " + howMany + " 种解法");
System.out.print("八皇后问题的结果为:");
for (int e : columns) {
System.out.print(e);
}
System.out.println("\n具体的图示如下图所示:");
for (row = 0; row < squares; row++) {
for (col = 0; col < squares; col++) {
if (col == positionInRow[row]) {
System.out.print("@");
} else {
System.out.print("*");
}
}
System.out.println();
}
System.out.println();
}
public void putQueen()
{
int row=0, col;
while (true)
{
for (col = p + 1; col < squares; col++)
{
if(rows[row]==AVAILABLE&&column[col]==AVAILABLE&&leftDiagonal[row+col]==AVAILABLE&&rightDiagonal[row-col+norm]==AVAILABLE)
{
break;
}
}
//在当前的行里面找到了可以放置皇后的位置
if(col<squares)
{
rows[row]=!AVAILABLE;
column[col]=!AVAILABLE;
leftDiagonal[row+col]=!AVAILABLE;
rightDiagonal[row-col+norm]=!AVAILABLE;
positionInRow[row]=col;
p=col;
}else//如果当前行没办反放置皇后了,那么回溯
{
if(row>0)//到前一行
{
row--;
p=positionInRow[row];
rows[row]=AVAILABLE;
column[p]=AVAILABLE;
leftDiagonal[row+p]=AVAILABLE;
rightDiagonal[row-p+norm]=AVAILABLE;
positionInRow[row]=-1;
continue;
}else
{
break;
}
}
if(row==squares-1)
{
howMany+=1;
printResults(positionInRow);
p=positionInRow[row];
rows[row]=AVAILABLE;
column[p]=AVAILABLE;
leftDiagonal[row+p]=AVAILABLE;
rightDiagonal[row-p+norm]=AVAILABLE;
positionInRow[row]=-1;
continue;
}
else
{
row++;
p=-1;
continue;
}
}
}
public static void main(String args[])
{
EightQueensNotRecursive eightQueens=new EightQueensNotRecursive();
eightQueens.putQueen();
System.out.println("皇后问题一共有"+howMany+"种解法");
}
}
递归方式:
package EightQueens;
public class EightQueensRecursive {
private static final boolean AVAILABLE=true;
private int squares=8,norm=squares-1;
private int positionInRow[]=new int[squares];
private boolean[] column=new boolean[squares];
private boolean[] leftDiagonal=new boolean[2*squares-1];
private boolean[] rightDiagonal=new boolean[2*squares-1];
private static int howMany=0;
public EightQueensRecursive(){
//To complete the initialization work for the column,leftDiagonal,rigthDiagonal.
for(int i=0;i<squares;i++){
column[i]=AVAILABLE;
positionInRow[i]=-1;
}
for(int i=0;i<2*squares-1;i++){
leftDiagonal[i]=AVAILABLE;
rightDiagonal[i]=AVAILABLE;
}
}
public void printResults(int[] columns){
int row,col;
System.out.println("八皇后问题的第 "+howMany+" 种解法");
System.out.print("八皇后问题的结果为:");
for(int e:columns){
System.out.print(e);
}
System.out.println("\n具体的图示如下图所示:");
for(row=0;row<squares;row++){
for(col=0;col<squares;col++){
if(col==positionInRow[row]){
System.out.print("@");
}else{
System.out.print("*");
}
}
System.out.println();
}
System.out.println();
}
public void putQueen(int row){
//如果前面已经得到了一个可行解
for(int i=0;i<squares;i++)
{
if(row>squares-1) break;
if(column[i]==AVAILABLE&&leftDiagonal[row+i]==AVAILABLE&&rightDiagonal[row-i+norm]==AVAILABLE)
{
positionInRow[row]=i;
column[i]=!AVAILABLE;
leftDiagonal[row+i]=!AVAILABLE;
rightDiagonal[row-i+norm]=!AVAILABLE;
if(row<squares-1){
putQueen(row+1);
}else
{
howMany+=1;
printResults(positionInRow);
}
column[i]=AVAILABLE;
leftDiagonal[row+i]=AVAILABLE;
rightDiagonal[row-i+norm]=AVAILABLE;
}
}
}
public static void main(String args[]){
EightQueensRecursive eightQueens=new EightQueensRecursive();
eightQueens.putQueen(0);
System.out.println("皇后问题一共找到了 "+howMany+"组解。");
}
}
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- 19、Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition 在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者 ...
- 【剑指offer】递归循环两种方式反转链表
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25737023 本文分别用非递归和递归两种方式实现了链表的反转,在九度OJ上AC. 题目描写 ...
- java 的对象拷贝(有深浅拷贝两种方式,深拷贝实现的两种方式(逐层实现cloneable接口,序列化的方式来实现))
Java提高篇--对象克隆(复制)(转自:http://www.cnblogs.com/Qian123/p/5710533.html#_label0) 阅读目录 为什么要克隆? 如何实现克隆 浅克 ...
- java的取出map里所有元素的两种方式
/* * 取出map元素的两种方式 */package com.map.test; import java.util.HashMap;import java.util.Iterator;import ...
- 数据结构队列的java实现,包括线性和链式两种方式
实现的思路为: 采用泛型的方式,首先定义了一个Queue的接口,然后通过实现该接口实现了线性和链式的两种形式的队列: 接口代码如下: package com.peter.java.dsa.interf ...
- Java多线程:常用的实现多线程的两种方式
之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程池的内容,我们以后会详细介绍;现在,先对的Thread和Runnable进行了解.本章内 ...
- Java中将xml文件转化为json的两种方式
原文地址https://blog.csdn.net/a532672728/article/details/76312475 最近有个需求,要将xml转json之后存储在redis中,找来找去发现整体来 ...
- Java并发--线程间协作的两种方式:wait、notify、notifyAll和Condition
在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...
随机推荐
- [windows]利用IPSec对指定的ip进行访问限制
以win2003系统为例: 操作(看图): 1.任务:现在192.168.2.200可访问;目的;本地禁止对其访问 2.进入:管理工具->本地安全设置->IP安全策略 3.右键创建IP安全 ...
- Effective Java 15 Minimize mutability
Use immutable classes as much as possible instead of mutable classes. Advantage Easy to design, impl ...
- Java文件IO操作应该抛弃File拥抱Paths和Files
Java7中文件IO发生了很大的变化,专门引入了很多新的类: import java.nio.file.DirectoryStream;import java.nio.file.FileSystem; ...
- MongoDB 存储引擎Wiredtiger原理剖析
今天开始看MongoDB 3.2的文档,发现了这么两句话 Support for Multiple Storage Engines MongoDB supports multiple storage ...
- 一次简单的MySQL数据库导入备份
任务目的:把现网数据库(MySQL5.5,windows)中的内容导入到测试数据库(MySQL5.1,linux)中 1.由于对MySQL并不熟悉,一上来我先考虑方案是用现成的数据库管理工具来处理.我 ...
- 设计模式C#实现(二)——适配器模式
适配器模式:将一个类的接口,转换成客户期望的另一个接口.适配器让原本接口不兼容的类可以合作无间. 如果它走起路来像只鸭子,叫起来像只鸭子,那么它必定可能是一只鸭子包装了鸭子适配器的火鸡…… 最近有一个 ...
- echo
echo $echo [-e] [内容字符串]显示后面的内容,缺省选项表示将后面的内容原模原样的显示出来,如果后面接的字符串不用"",会默认以空格为分隔符输出多个串 可以配合She ...
- Tomcat 内存和线程配置优化
1. tomcat 的线程配置参数详情如下: 修改conf/server.xml中的<Connector .../> 节点如下: <Connector port="8080 ...
- 聚合数据全国天气预报api接口
查询天气预报在APP中常用的一个常用功能,聚合数据全国天气预报api接口可以根据根据城市名/id查询天气.根据IP查询天气.据GPS坐标查询天气.查询城市天气三小时预报,并且支持全国不同城市天气预报查 ...
- 【NOIP合并果子】uva 10954 add all【贪心】——yhx
Yup!! The problem name reects your task; just add a set of numbers. But you may feel yourselvesconde ...