循环方式:
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
在前面我们将了很多关于同步的问题,然而在现实中,需要线程之间的协作.比如说最经典的生产者-消费者模型:当队列满时,生产者需要等待队列有空间才能继续往里面放入商品,而在等待的期间内,生产者必须释放对临界 ...
随机推荐
- FQ技术
什么是技术,就是翻_墙技术啦~ 关于GoogleFQ的网页,点击图片就能进入该网页浏览相关的FQ的设置 hosts所在文件夹: Windows 系统hosts位于 C:\Windows\System3 ...
- C# 3个线程A B C 依次打印123123123..
C#经典面试题: 有3个线程,A线程打印1,B线程打印2,C线程打印3,请用程序实现依次打印123123123... class Program { static void Main(string[] ...
- python yield
http://www.jb51.net/article/15717.htm 这里还不错 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist(alist) ...
- Effective Java 19 Use interfaces only to define types
Reason The constant interface pattern is a poor use of interfaces. That a class uses some constants ...
- Java Concurrency In Practice - Chapter 1 Introduction
1.1. A (Very) Brief History of Concurrency motivating factors for multiple programs to execute simul ...
- CentOS 7 安装Docker
1.安装前检查: a.内核版本 uname -a b.检查Device Mapper ls -l /sys/class/misc/device-mapper 2.安装Docker: a.更新系统包到最 ...
- jQuery form插件的使用--处理server返回的JSON, XML,HTML数据
详细代码: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> & ...
- jsp页面img利用tomcat配置访问服务器绝对路径显示图片
1.打开tomcat下的server.xml文件,路径\apache-tomcat-7.0.62\conf文件夹下. 2.下<host></host>加入<Context ...
- nyoj 170 网络的可靠性
题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=170 思路:统计每个节点的度,将度为1的节点消去所需要的最少的边即为答案. 代码: #in ...
- 【Android Demo】通过WebService获取今日天气情况
因为本身是在搞.NET方面的东东,现在在学习Android,所以想实现Android通过WebService接口来获取数据,网上很多例子还有有问题的.参考:Android 通过WebService进行 ...