八皇后(Java)不解决第一线

时间:2015-03-19 18:41:11

标签: java backtracking

我的编码有问题,我似乎无法找到问题,为什么我的代码的第一行打印出一行零。在我的主要方法上,如果我放置0,第一行返回false,因此它打印出“未找到解决方案”,但如果我使用1则打印出整个板,但第一行只打印出零。我认为我的回溯方法有问题,但我不确定。有人可以检查我的代码并解释我是否做错了什么?

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");
        }

    }


}

1 个答案:

答案 0 :(得分:0)

变化 int row = 1;至:int row = 0;

变化 if(Q.placeQueens(1)){至:if(Q.placeQueens(0)){