Java 使用堆栈和回溯的N皇后程序
我正在尝试编写一个程序(用于家庭作业),该程序有一个nxn板,我需要算法来找到所有可能的解决方案,使n个皇后处于一个位置,其中没有一个皇后可以捕捉彼此。我现在的代码是这样的Java 使用堆栈和回溯的N皇后程序,java,stack,backtracking,Java,Stack,Backtracking,我正在尝试编写一个程序(用于家庭作业),该程序有一个nxn板,我需要算法来找到所有可能的解决方案,使n个皇后处于一个位置,其中没有一个皇后可以捕捉彼此。我现在的代码是这样的 import java.util.Stack; public class NQueens { public static int solve(int n) { Stack<Integer> s = new Stack<Integer>(); int current
import java.util.Stack;
public class NQueens {
public static int solve(int n) {
Stack<Integer> s = new Stack<Integer>();
int current = 0;
int numSolutions = 0;
while(!(current>n)){
if(s.size() == n){
break;
}
if(current == n){
if((s.peek()==n) && (s.size() == 1)){
break;
}
if(s.peek() == n){
s.pop();
current = s.pop()+1;
}
else{
current = s.pop()+1;
}
}
else if(validPositionChecker(s, current)){
s.push(current);
current = 0;
}
else{
current++;
}
}
if(s.size()==n){
printSolution(s);
numSolutions++
}
return numSolutions;
}
public static boolean validPositionChecker(Stack<Integer> s, int currentPosition) {
for (int i = 0; i < s.size(); i++) {
if (s.get(i) == currentPosition){
return false;
}
if ((s.get(i) - currentPosition) == (s.size() - i)){
return false;
}
if ((currentPosition - s.get(i)) == (s.size() - i)){
return false;
}
}
return true;
}
//this method prints out a solution from the current stack
private static void printSolution(Stack<Integer> s) {
for (int i = 0; i < s.size(); i ++) {
for (int j = 0; j < s.size(); j ++) {
if (j == s.get(i))
System.out.print("Q ");
else
System.out.print("* ");
}
System.out.println();
}
System.out.println();
}//printSolution()
public static void main(String[] args) {
int n = 8;
// pass in parameter n from command line
if (args.length == 1) {
n = Integer.parseInt(args[0].trim());
if (n < 1) {
System.out.println("Incorrect parameter");
System.exit(-1);
}//if
}//if
int number = solve(n);
System.out.println("There are " + number + " solutions to the " + n + "-queens problem.");
}//main()
}
import java.util.Stack;
公开课{
公共静态整数求解(整数n){
堆栈s=新堆栈();
int电流=0;
int numSolutions=0;
而(!(当前>n)){
如果(s.size()==n){
打破
}
如果(当前==n){
如果((s.peek()==n)和&(s.size()==1)){
打破
}
如果(s.peek()==n){
s、 pop();
电流=s.pop()+1;
}
否则{
电流=s.pop()+1;
}
}
else if(有效位置检查器,当前)){
s、 推动(电流);
电流=0;
}
否则{
电流++;
}
}
如果(s.size()==n){
印刷液;
numSolutions++
}
返回numSolutions;
}
公共静态布尔值validPositionChecker(堆栈s,int currentPosition){
对于(int i=0;i
为了解释这段代码。这个代码有效。但它只打印出一个解决方案。在8x8的默认nxn板中,它应该有92个独特的解决方案
我的问题是如何让它打印所有的解决方案。我知道在求解方法中,我可以在给定的while循环的基础上使用另一个while,但我不知道给while循环什么参数才能退出。基本上重申一下,我需要知道在什么条件下,当找到所有的解决方案时,我知道更大的while循环何时会停止
我必须使用堆栈,我不能在这个作业中使用递归。递归会使它简单得多 import java.util.Stack;
import java.util.Stack;
public class QueenSolver {
private static final int NUM_QUEENS = 8;
private static int[] board = new int[NUM_QUEENS];
public static void solve(int n) {
int current = 0;
int numSolutions = 0;
Stack<Integer> s = new Stack<>();
while (true) {
while (current < n) {
if (isBoardCorrect(s, current)) {
s.push(current);
board[s.size() - 1] = current;
current = 0;
} else {
current++;
}
}
if (s.size() == n) {
printSolution(++numSolutions);
}
if (s.isEmpty()) {
break;
}
if (s.peek() == n) {
s.pop();
}
current = s.pop() + 1;
}
}
private static boolean isBoardCorrect(Stack<Integer> s, int currentPosition) {
for (int i = 0; i < s.size(); i++) {
if (board[i] == currentPosition
|| currentPosition == board[i]
|| Math.abs(currentPosition - board[i]) == s.size() - i) {
return false;
}
}
return true;
}
private static void printSolution(int num) {
System.out.print(num + ": ");
for (int i = 0; i < board.length; i++) {
System.out.print("(" + i + "," + board[i] + ")");
if (i < board.length - 1) {
System.out.print(", ");
}
}
System.out.println();
}
public static void main(String[] args) {
QueenSolver.solve(8);
}
}
公共类QueenSolver{
私有静态final int NUM_QUEENS=8;
私有静态int[]板=新int[NUM_QUEENS];
公共静态void solve(int n){
int电流=0;
int numSolutions=0;
堆栈s=新堆栈();
while(true){
而(电流
要继续操作,需要将第一个皇后放置在(n+1)
位置。不应添加另一个循环,它会再次找到相同的解决方案。你不需要在找到第一个解决方案时终止,而是继续找到其他解决方案。所以,好吧,我这样做是为了,一旦它找到一个解决方案,它将弹出最后一键,并尝试找到下一个解决方案。然而,循环最终会停止吗?如果它找到了所有的解决方案,循环会中断吗?事实上,我找到了。非常感谢!我希望我能给你的不仅仅是评论和投票。