八皇后(Java)不解决第一行问题
我的编码有问题,我似乎找不到为什么我的代码的第一行打印出一行零的问题。在我的主方法中,如果我放置一个0,第一行返回false,所以它打印出“未找到解决方案”,但是如果我使用1,那么它打印出整个电路板,但是第一行只打印出零。我相信我的回溯方法有问题,但我不确定。有人能检查我的代码并解释我是否做错了什么吗八皇后(Java)不解决第一行问题,java,backtracking,Java,Backtracking,我的编码有问题,我似乎找不到为什么我的代码的第一行打印出一行零的问题。在我的主方法中,如果我放置一个0,第一行返回false,所以它打印出“未找到解决方案”,但是如果我使用1,那么它打印出整个电路板,但是第一行只打印出零。我相信我的回溯方法有问题,但我不确定。有人能检查我的代码并解释我是否做错了什么吗 public class Queens { //variable for 8 by 8 board public static final int BOARD_SIZE = 8;
public class Queens
{
//variable for 8 by 8 board
public static final int BOARD_SIZE = 8;
//use varaible to empty a place or the whole board
public static final int EMPTY = 0;
//Variable to
public static final int QUEEN = 1;
private int board[][]; //double array to create board.
public Queens()
{
//constructor for board
board = new int[BOARD_SIZE][BOARD_SIZE];
}
public boolean placeQueens(int column)
{
if(column >= BOARD_SIZE)
{
return true; //if true problem solved!
}
else
{
boolean queenPlaced = false;
int row = 1; // number of square in column
while( !queenPlaced && (row < BOARD_SIZE))
{
//if square not under attack, continue to set queen to spot
if (!isUnderAttack(row, column))
{
setQueen(row, column);
queenPlaced = placeQueens(column+1);
//if no queen is possible in next column
if(!queenPlaced)
{
//backtrack
removeQueen(row, column);
}
}
row++;
}
return queenPlaced;
}
}
//clears the board
public void clearBoard()
{
for(int row = 0; row < BOARD_SIZE; row++)
{
for (int column = 0; column < BOARD_SIZE; column++)
{
board[row][column] = EMPTY;
}
}
}
//Displays the board
public void displayBoard()
{
for (int row = 0; row < BOARD_SIZE; row++)
{
System.out.println(" ");
for (int column = 0; column < BOARD_SIZE; column++)
{
System.out.print(board[row][column] + " ");
}
}
}
//Sets a queen at square indicated by row and column
private void setQueen(int row, int column)
{
board[row][column] = QUEEN;
}
//Removes the Queen for backtracking
private void removeQueen(int row, int column)
{
board[row][column] = EMPTY;
}
//Checks to see if Queen is under attack by any other of the previous Queens
private boolean isUnderAttack(int row, int column)
{
for (int i = 0; i < BOARD_SIZE; i++)
{
if (board[row][i] == QUEEN) //check attack from above
{
return true;
}
int x1 = row - column + i;
if (0 <= x1 && x1 < BOARD_SIZE && board[x1][i] == QUEEN) //check diagonal left up
{
return true;
}
int x2 = row + column - i;
if (0 <= x2 && x2 < BOARD_SIZE && board[x2][i] == QUEEN) // check diagonal left down
{
return true;
}
}
return false;
}
private int index(int number)
{
return number;
}
//main to test program
public static void main(String[] args)
{
Queens Q = new Queens(); //create board
Q.clearBoard(); // clear board before anything
if(Q.placeQueens(1)){
Q.displayBoard();
}
else
{
System.out.println("No Solution Found");
}
}
}
公共类皇后区
{
//8×8板的变量
公共静态最终int板尺寸=8;
//使用varaible清空一个地方或整个板
公共静态final int EMPTY=0;
//可变到
公共静态最终整数=1;
专用int板[][];//创建板的双数组。
公众皇后区()
{
//线路板建造师
线路板=新的int[线路板尺寸][线路板尺寸];
}
公共布尔PlaceQueen(int列)
{
如果(列>=电路板尺寸)
{
返回true;//如果问题为true,则返回true!
}
其他的
{
布尔queenPlaced=false;
int row=1;//列中的平方数
而(!queenPlaced&&(行<板大小))
{
//如果方阵没有受到攻击,继续将女王置于攻击点
如果(!isUnderAttack(行、列))
{
setQueen(行、列);
queenPlaced=placeQueens(列+1);
//如果在下一列中没有皇后是可能的
如果(!queenPlaced)
{
//回溯
removeQueen(行、列);
}
}
行++;
}
返回皇后区;
}
}
//清场
公共空白结算板()
{
用于(int row=0;row<线路板大小;row++)
{
用于(int column=0;columnint row=1;
to:int row=0;
及
改变
if(Q.placeQueens(1)){
to:if(Q.placeQueens(0)){
我真的建议使用调试器缩小您的问题范围,然后用偏离预期值的特定区域更新问题。我们不会为您搜索每一行!您的问题在placeQueens()。当第一行应该为0时,您将其设置为1。您能推荐一个调试器吗?或者在运行时跟踪问题的某种方法吗?@Frecklefoot如果使用0,则返回“未找到解决方案”因为第一行没有queen。Eclipse是免费的,在Java中非常流行。好吧,我想我误解了你的代码逻辑。但是如果第一行没有queen,你不希望它都是零吗?祝你调试顺利。