Java Can';从我的8-Queen算法代码中找不到bug
下面是我的八皇后问题代码。我相信它应该能工作,但代码没有给我正确的答案。我花了好几个小时才找到这个bug,但是我没能找到它。我非常想知道哪里出了问题。请帮忙Java Can';从我的8-Queen算法代码中找不到bug,java,algorithm,data-structures,Java,Algorithm,Data Structures,下面是我的八皇后问题代码。我相信它应该能工作,但代码没有给我正确的答案。我花了好几个小时才找到这个bug,但是我没能找到它。我非常想知道哪里出了问题。请帮忙 public class EightQueen { private boolean[][] queenOnSquare; private boolean[] colEmpty; private boolean[] upDiagEmpty; private boolean[] downDiagEmpty; private int numOfS
public class EightQueen {
private boolean[][] queenOnSquare;
private boolean[] colEmpty;
private boolean[] upDiagEmpty;
private boolean[] downDiagEmpty;
private int numOfSol;
private int bordersize;
public EightQueen(int size){
//Initialize the object
queenOnSquare=new boolean[size][size];
colEmpty=new boolean[size];
upDiagEmpty=new boolean[2*size-2];
downDiagEmpty=new boolean[2*size-2];
bordersize=size;
numOfSol=0;
for (int i=0;i<bordersize;i++){
colEmpty[i]=true;
upDiagEmpty[i]=true;
downDiagEmpty[i]=true;
}
}
public static void main(String[] args) {
//Create a new object of 8 queens
EightQueen obj=new EightQueen(8);
obj.solve();
obj.printNumOfSol();
}
//This is just a wrapper method to study the recusion
public void solve(){
findSolutions(0);
}
//This is the classic recursive backtracking
public void findSolutions(int row){
if (row==bordersize){
numOfSol++;
printSol();
}
for (int col=0;col<bordersize;col++){
if (isSafe(row,col)){
placeQueen(row,col);
findSolutions(row+1);
removeQueen(row,col);
}
}
}
//Place the queen
public void placeQueen(int row, int col) {
queenOnSquare[row][col] = true;
colEmpty[col] = false;
upDiagEmpty[row + col] = false;
downDiagEmpty[(bordersize - 1) + row - col] = false;
}
//Remove the queen
public void removeQueen(int row, int col) {
queenOnSquare[row][col] = false;
colEmpty[col] = true;
upDiagEmpty[row + col] = true;
downDiagEmpty[(bordersize - 1) + row - col] = true;
}
//Judge if a new queen can be placed this spot
public boolean isSafe(int row, int col) {
return (colEmpty[col]
&& upDiagEmpty[row + col]
&& downDiagEmpty[(bordersize - 1) + row - col]);
}
//Print the solution
public void printSol(){
System.out.println("Here is one solution:\n");
for (int i=0;i<bordersize;i++){
for (int j=0;j<bordersize;j++){
if (queenOnSquare[i][j]) System.out.print("Q ");
System.out.print(". ");
}
System.out.println();
}
}
//Print the total number of solutions
public void printNumOfSol(){
System.out.println("The number of solutions is: "+numOfSol);
}
}
public class-EightQueen{
私有布尔[][]平方;
私有布尔[]空;
私有布尔[]上空;
私有布尔[]空;
纽莫夫索尔私人酒店;
私人国际贸易规模;
公共八码(整数大小){
//初始化对象
queenOnSquare=新布尔值[size][size];
colEmpty=新布尔值[大小];
upDiagEmpty=新布尔值[2*size-2];
downDiagEmpty=新布尔值[2*size-2];
边界大小=大小;
numOfSol=0;
对于(int i=0;i每个用户3340630,upDiagEmpty和downDiagEmpty的条目数都是您正在初始化的条目数的两倍。将它们更改为“upDiagBlocked”和“downDiagBlocked”可能更有意义,因此您不需要初始化(默认值为false)当它们被阻止时,将它们设置为true。但实际上,您只需要将这些数组完全初始化为true。Hej
上面的评论是正确的:要求人们发现代码中的错误并不是特别有效。最好尽可能缩小问题范围,然后更具体地提问
尽管如此:实际上,构造函数和边界中的初始化是不正确的
我想如果你把构造函数修改成
public EightQueen(int size) {
//Initialize the object
queenOnSquare=new boolean[size][size];
colEmpty=new boolean[size];
upDiagEmpty=new boolean[2*size-1];
downDiagEmpty=new boolean[2*size-1];
bordersize=size;
numOfSol=0;
for (int i=0;i<bordersize;i++){
colEmpty[i]=true;
}
for (int i=0;i<2*bordersize-2;i++){
upDiagEmpty[i]=true;
downDiagEmpty[i]=true;
}
}
public-EightQueen(整数大小){
//初始化对象
queenOnSquare=新布尔值[size][size];
colEmpty=新布尔值[大小];
upDiagEmpty=新布尔值[2*size-1];
downDiagEmpty=新布尔值[2*size-1];
边界大小=大小;
numOfSol=0;
对于(int i=0;i分别测试每个方法,请尝试使用调试器或尝试使用print statements.Hi。要求人们发现代码中的错误并不特别有效。您应该使用调试器(或添加print statements)通过跟踪程序的进度,并将其与预期发生的情况进行比较,来隔离问题。一旦两者出现分歧,您就发现了问题。(如果必要,您应该构建一个解决方案。)您没有正确初始化变量。我还建议您使用printf调试或调试器。什么使您认为您的代码是错误的?您的代码与完美和预期的程序有多大不同?diag数组需要大小2*size-1
而不是2*size-2
,因为它们需要容纳值row+col
wh如果大小为8,ich可能会上升到14。