gpt4 book ai didi

java - N-皇后程序,带堆栈

转载 作者:行者123 更新时间:2023-12-01 13:21:30 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序将返回 N 皇后问题的解决方案的数量。该代码使用堆栈来跟踪有效的皇后位置,并适本地弹出和推送。但代码中的某些部分永远无法到达。我相信它们是程序无法运行的原因。它们已标有感叹号。谁能解释为什么这些部分从未达到?

import java.util.Stack;

public class nQ {

public static Stack<Integer> s = new Stack<Integer>();
public static int n;
public static int total;
public static int i;

//finds and prints out all solutions to the n-queens problem
public static int solve(int n) {

// i goes through each row to place a queen
// x goes through the columns within each row
System.out.println("1");
for(int i = 0; i < n; i++) {
System.out.println("2");
for(int x = 0; x<n; x++){
System.out.println("3");
if(conflict(x) == false){ // loop through each column and checks whether it conflicts with current position of queen
System.out.println("4");
s.push(x); // no conflict, push x
//Q = s.get(x); // set current position of queen
break; //break out of loop to move on to next row, i++
}

else if (conflict(x)==true){
System.out.println("5");
if(s.isEmpty() == true){
System.out.println("!!!!6");
break;
}

if(x==n-1){ // if its looped through all columns, and there's no valid position
s.pop(); //pop last entry
i= i-1; // and backtrack to previous row, to find another valid position for q in previous row
System.out.println("7");
}

}

if (s.size()==n){ // if stack size is n, then stack is full and a solution has been found
total++;
System.out.print(s);// print solution
s.pop();
i= i-1; //backtrack to find next solution
System.out.println("!!!!!!!8");
}
System.out.println("9");
}
System.out.println("10");

}
System.out.println("11");
System.out.print(s);
return total;
}

public static boolean conflict(int x) {

for (int j = 0; j<s.size(); j++) {
if ((s.get(j)==x) || ((x-s.get(j))== (i-(j))) || ((s.get(j)-x)== (i-(j)))) {
return true; //there is a conflict!! x
}
}
return false; //no conflict
}



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("* ");
}//for
System.out.println();
}//for
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;
public class nQ {
public static Stack<Integer> s = new Stack<Integer>();
public static int n;
public static int total;
public static int i;

//finds and prints out all solutions to the n-queens problem
public static int solve(int n) {

// i goes through each row to place a queen
// x goes through the columns within each row
for(int i = 0; i < n; i++) {
for(int x = 0; x<n; x++){
System.out.println("3 x=" + x + ", n=" + n + ", s.size()=" + s.size());
if(conflict(x) == false){ // loop through each column and checks whether it conflicts with current position of queen
s.push(x); // no conflict, push x
System.out.println("4 s.size()=" + s.size());
//Q = s.get(x); // set current position of queen
break; //break out of loop to move on to next row, i++
}

else if (conflict(x)==true){
System.out.println("5 x=" + x + ", n=" + n + ", s.size()=" + s.size());
if(s.isEmpty() == true){
System.out.println("!!!!6");
break;
}

if(x==n-1){ // if its looped through all columns, and there's no valid position
s.pop(); //pop last entry
System.out.println("6 x=" + x + ", n=" + n + ", s.size()=" + s.size());
i= i-1; // and backtrack to previous row, to find another valid position for q in previous row
System.out.println("7");
}

}

System.out.println("7a x=" + x + ", n=" + n + ", s.size()=" + s.size());

if (s.size()==n){ // if stack size is n, then stack is full and a solution has been found
total++;
System.out.print(s);// print solution
s.pop();
i= i-1; //backtrack to find next solution
System.out.println("!!!!!!!8");
}
}

}
System.out.print(s);
return total;
}

public static boolean conflict(int x) {

for (int j = 0; j<s.size(); j++) {
if ((s.get(j)==x) || ((x-s.get(j))== (i-(j))) || ((s.get(j)-x)== (i-(j)))) {
return true; //there is a conflict!! x
}
}
return false; //no conflict
}



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("* ");
}//for
System.out.println();
}//for
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()

}

输出:

[R:\jeffy\programming\sandbox\xbnjava]java nQ
3 x=0, n=8, s.size()=0
4 s.size()=1
3 x=0, n=8, s.size()=1
5 x=0, n=8, s.size()=1
7a x=0, n=8, s.size()=1
3 x=1, n=8, s.size()=1
4 s.size()=2
3 x=0, n=8, s.size()=2
5 x=0, n=8, s.size()=2
7a x=0, n=8, s.size()=2
3 x=1, n=8, s.size()=2
5 x=1, n=8, s.size()=2
7a x=1, n=8, s.size()=2
3 x=2, n=8, s.size()=2
5 x=2, n=8, s.size()=2
7a x=2, n=8, s.size()=2
3 x=3, n=8, s.size()=2
4 s.size()=3
3 x=0, n=8, s.size()=3
5 x=0, n=8, s.size()=3
7a x=0, n=8, s.size()=3
3 x=1, n=8, s.size()=3
5 x=1, n=8, s.size()=3
7a x=1, n=8, s.size()=3
3 x=2, n=8, s.size()=3
5 x=2, n=8, s.size()=3
7a x=2, n=8, s.size()=3
3 x=3, n=8, s.size()=3
5 x=3, n=8, s.size()=3
7a x=3, n=8, s.size()=3
3 x=4, n=8, s.size()=3
4 s.size()=4
3 x=0, n=8, s.size()=4
5 x=0, n=8, s.size()=4
7a x=0, n=8, s.size()=4
3 x=1, n=8, s.size()=4
5 x=1, n=8, s.size()=4
7a x=1, n=8, s.size()=4
3 x=2, n=8, s.size()=4
5 x=2, n=8, s.size()=4
7a x=2, n=8, s.size()=4
3 x=3, n=8, s.size()=4
5 x=3, n=8, s.size()=4
7a x=3, n=8, s.size()=4
3 x=4, n=8, s.size()=4
5 x=4, n=8, s.size()=4
7a x=4, n=8, s.size()=4
3 x=5, n=8, s.size()=4
5 x=5, n=8, s.size()=4
7a x=5, n=8, s.size()=4
3 x=6, n=8, s.size()=4
4 s.size()=5
3 x=0, n=8, s.size()=5
5 x=0, n=8, s.size()=5
7a x=0, n=8, s.size()=5
3 x=1, n=8, s.size()=5
5 x=1, n=8, s.size()=5
7a x=1, n=8, s.size()=5
3 x=2, n=8, s.size()=5
5 x=2, n=8, s.size()=5
7a x=2, n=8, s.size()=5
3 x=3, n=8, s.size()=5
5 x=3, n=8, s.size()=5
7a x=3, n=8, s.size()=5
3 x=4, n=8, s.size()=5
5 x=4, n=8, s.size()=5
7a x=4, n=8, s.size()=5
3 x=5, n=8, s.size()=5
5 x=5, n=8, s.size()=5
7a x=5, n=8, s.size()=5
3 x=6, n=8, s.size()=5
5 x=6, n=8, s.size()=5
7a x=6, n=8, s.size()=5
3 x=7, n=8, s.size()=5
5 x=7, n=8, s.size()=5
6 x=7, n=8, s.size()=4
7
7a x=7, n=8, s.size()=4
3 x=0, n=8, s.size()=4
5 x=0, n=8, s.size()=4
7a x=0, n=8, s.size()=4
3 x=1, n=8, s.size()=4
5 x=1, n=8, s.size()=4
7a x=1, n=8, s.size()=4
3 x=2, n=8, s.size()=4
5 x=2, n=8, s.size()=4
7a x=2, n=8, s.size()=4
3 x=3, n=8, s.size()=4
5 x=3, n=8, s.size()=4
7a x=3, n=8, s.size()=4
3 x=4, n=8, s.size()=4
5 x=4, n=8, s.size()=4
7a x=4, n=8, s.size()=4
3 x=5, n=8, s.size()=4
5 x=5, n=8, s.size()=4
7a x=5, n=8, s.size()=4
3 x=6, n=8, s.size()=4
4 s.size()=5
3 x=0, n=8, s.size()=5
5 x=0, n=8, s.size()=5
7a x=0, n=8, s.size()=5
3 x=1, n=8, s.size()=5
5 x=1, n=8, s.size()=5
7a x=1, n=8, s.size()=5
3 x=2, n=8, s.size()=5
5 x=2, n=8, s.size()=5
7a x=2, n=8, s.size()=5
3 x=3, n=8, s.size()=5
5 x=3, n=8, s.size()=5
7a x=3, n=8, s.size()=5
3 x=4, n=8, s.size()=5
5 x=4, n=8, s.size()=5
7a x=4, n=8, s.size()=5
3 x=5, n=8, s.size()=5
5 x=5, n=8, s.size()=5
7a x=5, n=8, s.size()=5
3 x=6, n=8, s.size()=5
5 x=6, n=8, s.size()=5
7a x=6, n=8, s.size()=5
3 x=7, n=8, s.size()=5
5 x=7, n=8, s.size()=5
6 x=7, n=8, s.size()=4
7
7a x=7, n=8, s.size()=4
3 x=0, n=8, s.size()=4
5 x=0, n=8, s.size()=4
7a x=0, n=8, s.size()=4
3 x=1, n=8, s.size()=4
5 x=1, n=8, s.size()=4
7a x=1, n=8, s.size()=4
3 x=2, n=8, s.size()=4
5 x=2, n=8, s.size()=4
7a x=2, n=8, s.size()=4
3 x=3, n=8, s.size()=4
5 x=3, n=8, s.size()=4
7a x=3, n=8, s.size()=4
3 x=4, n=8, s.size()=4
5 x=4, n=8, s.size()=4
7a x=4, n=8, s.size()=4
3 x=5, n=8, s.size()=4
5 x=5, n=8, s.size()=4
7a x=5, n=8, s.size()=4
3 x=6, n=8, s.size()=4
4 s.size()=5
3 x=0, n=8, s.size()=5
5 x=0, n=8, s.size()=5
7a x=0, n=8, s.size()=5
3 x=1, n=8, s.size()=5
5 x=1, n=8, s.size()=5
7a x=1, n=8, s.size()=5
3 x=2, n=8, s.size()=5
5 x=2, n=8, s.size()=5
7a x=2, n=8, s.size()=5
3 x=3, n=8, s.size()=5
5 x=3, n=8, s.size()=5
7a x=3, n=8, s.size()=5
3 x=4, n=8, s.size()=5
5 x=4, n=8, s.size()=5
7a x=4, n=8, s.size()=5
3 x=5, n=8, s.size()=5
5 x=5, n=8, s.size()=5
7a x=5, n=8, s.size()=5
3 x=6, n=8, s.size()=5
5 x=6, n=8, s.size()=5
7a x=6, n=8, s.size()=5
3 x=7, n=8, s.size()=5
5 x=7, n=8, s.size()=5
6 x=7, n=8, s.size()=4
7
7a x=7, n=8, s.size()=4
3 x=0, n=8, s.size()=4
5 x=0, n=8, s.size()=4
7a x=0, n=8, s.size()=4
3 x=1, n=8, s.size()=4
5 x=1, n=8, s.size()=4
7a x=1, n=8, s.size()=4
3 x=2, n=8, s.size()=4
5 x=2, n=8, s.size()=4
7a x=2, n=8, s.size()=4
3 x=3, n=8, s.size()=4
5 x=3, n=8, s.size()=4
7a x=3, n=8, s.size()=4
3 x=4, n=8, s.size()=4
5 x=4, n=8, s.size()=4
7a x=4, n=8, s.size()=4
3 x=5, n=8, s.size()=4
5 x=5, n=8, s.size()=4
7a x=5, n=8, s.size()=4
3 x=6, n=8, s.size()=4
4 s.size()=5
[0, 1, 3, 4, 6]There are 0 solutions to the 8-queens problem.

关于java - N-皇后程序,带堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21995651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com