我的NQueens isSafeMove()函数不断返回false,我无法弄清楚原因。我相信它可能与我的checkLeft,checkUpperDiag或checkLowerDiag有关,但我没有看到它们有什么问题。 isSafeMove检查是否是女王的任何可移动距离中的一块,如果有返回false,如果没有则返回true。
import java.awt.Point;
public class NQueensModel
{
private int myNumQueens;
private boolean myBoard[][];
private boolean mySolutionBoard [][];
private String mySolutionBoardString [][];
private int myNumSolutions;
NQueensModel(int nQueens)
{
myNumQueens = nQueens;
myBoard = new boolean [myNumQueens][myNumQueens];
mySolutionBoard = new boolean [myNumQueens][myNumQueens];
mySolutionBoardString = new String [myNumQueens][myNumQueens];
}
public boolean solvePuzzle()
{
return solvePuzzle(0);
}
private boolean solvePuzzle(int ncolumn)
{
if (ncolumn >= myNumQueens)
{
return true;
}
for(int i = 0; i < myNumQueens; i++)
{
if(this.isSafeMove(i, ncolumn) == true)
{
this.placeQueen(i, ncolumn);
if(this.solvePuzzle(ncolumn + 1) == true)
{
return true;
}
this.removeQueen(i, ncolumn);
}
}
return false;
}
public boolean isSafeMove(int row, int col)
{
if(this.checkLeft(row, col) || this.checkUpperDiag(row, col)
|| this.checkLowerDiag(row, col) == true)
{
System.out.println("false");
return false;
}
else
{
System.out.println("true");
return true;
}
}
private boolean checkLeft(int row, int col)
{
if(col >= myNumQueens || row >= myNumQueens)
{
return false;
}
if(row == 0)
{
return true;
}
for(int i= col - 1 ; i >= 0; i--)
{
if(myBoard[row][i] == true)
{
return false;
}
if(i == 0)
{
return true;
}
}
return false;
}
private boolean checkUpperDiag(int row, int col)
{
if(col == 0 || row == 0)
{
return true;
}
for(int i = row, j = col; row >= 0 || col >= 0; row--, col--)
{
if(myBoard[row][col] == true)
{
return false;
}
if(col == 0 && row == 0)
{
return true;
}
}
return false;
}
private boolean checkLowerDiag(int row, int col)
{
if(col == 0 || row == myNumQueens - 1)
{
return true;
}
for(int i = row, j = col; row < myNumQueens || col < myNumQueens; row--, col++)
{
if(myBoard[row][col] == true)
{
return false;
}
if(col == 0 && row == myNumQueens - 1)
{
return true;
}
}
return false;
}
public boolean placeQueen(int row, int col)
{
if(myBoard[row][col] == false)
{
myBoard[row][col] = true;
return false;
}
return false;
}
public String getMySolutionBoardString()
{
for(int col = 0; col < myNumQueens; col++)
{
for(int row = 0; row < myNumQueens; row++)
{
if(myBoard[col][row] == true)
{
mySolutionBoardString[row][col] = "Q";
}
else
{
mySolutionBoardString[row][col] = "-";
}
System.out.print(mySolutionBoardString[row][col] + " ");
}
System.out.println("");
}
return null;
}
private boolean removeQueen(int row, int col)
{
myBoard[row][col] = false;
return false;
}
public boolean getQueenOnBoard(Point point)
{
return myBoard[(int)point.getX()][(int)point.getY()];
}
public String toString()
{
String string = this.getMySolutionBoardString();
return string;
}
}
答案 0 :(得分:0)
在你的循环中
for(int i = 0; i < myNumQueens; i++)
{
if(this.isSafeMove(i, ncolumn) == true)
{
this.placeQueen(i, ncolumn);
if(this.solvePuzzle(ncolumn + 1) == true)
{
return true;
}
this.removeQueen(i, ncolumn);
}
}
当你第一次调用isSafeMove(i,ncolumn)时想想它i = 0所以checkLeft将返回true并且在ncolumn = 0之后的每次迭代所以你对checkUpperDiag()和checkLowerDiag()的调用将返回true,所以那些方法每次都返回true意味着isSafeMove将在循环的每次迭代中返回false