主题:8个女王,当我打印出来时,它给我五份副本,我只想要一份?

时间:2010-12-19 16:53:21

标签: java

输出

Q   x   x   x   x   x   x   x   
    x   x   Q   x   x   x   x   

x   x   x   Q   x   x   x   x   
x   x   x   x   x   x   Q   x   
x   x   Q   x   x   x   x   x   
x   x   x   x   x   x   x   Q   
x   Q   x   x   x   x   x   x   
x   x   x   x   Q   x   x   x   
Q   x   x   x   x   x   x   x   
    x   x   x   x   Q   x   x   

x   x   x   Q   x   x   x   x   
x   Q   x   x   x   x   x   x   
x   x   x   x   x   x   Q   x   
x   x   Q   x   x   x   x   x   
x   x   x   x   x   Q   x   x   
x   x   x   x   x   x   x   Q   
x   x   x   x   Q   x   x   x   
Q   x   x   x   x   x   x   x   

x   x   x   x   Q   x   x   x   
x   Q   x   x   x   x   x   x   
x   x   x   Q   x   x   x   x   
x   x   x   x   x   x   Q   x   
x   x   Q   x   x   x   x   x   
x   x   x   x   x   x   x   Q   
x   x   x   x   x   Q   x   x   
Q   x   x   x   x   x   x   x   

x   x   Q   x   x   x   x   x   
x   x   x   x   Q   x   x   x   
x   Q   x   x   x   x   x   x   
x   x   x   x   x   x   x   Q   
x   x   x   x   x   Q   x   x   
x   x   x   Q   x   x   x   x   
x   x   x   x   x   x   Q   x   
Q   x   x   x   x   x   x   x   

x   x   Q   x   x   x   x   x   
x   x   x   x   x   Q   x   x   
x   x   x   Q   x   x   x   x   
x   Q   x   x   x   x   x   x   
x   x   x   x   x   x   x   Q   
x   x   x   x   Q   x   x   x   
x   x   x   x   x   x   Q   x   
Q   x   x   x   x   x   x   x   

代码

public class ChessV2

{
    static String[][] board = new String[8][8];


   public static void main(String[] args)

   {

           for(int i = 0; i < board.length; i++)
       {
                for(int j = 0; j < board.length; j++)
                {
                    board[i][j] = " ";
                }
       }

      placeQueens(0);    
    }


   public static boolean placeQueens(int column)

    {

        boolean placed = false;

        if(column == 8)
        {

            System.out.println();
            for(int i = 0; i < board.length; i++)
            {
                for(int j = 0; j < board.length; j++)
                {
                    System.out.print(board[i][j] + "   ");
                }
                System.out.println();

            }
            placed = false; 
        }





        else
        {
          for(int i = 0; i < 8; i++)
          {
             if(!checkForQueens(i,column))
             {
                board[i][column] = "Q";

                if(placeQueens(column + 1) == true)
                {
                     placed = true;

                }

                else
                {
                    placed = false;
                   board[i][column] = "x";
                    }
             }
          }
        }

        return placed;
    }


   public static boolean checkForQueens(int row, int column)

    {
        boolean placeTaken = false;
        int newCol =0;
        int newRow;

        for(newRow = row - 1; newRow >= 0; newRow--) // vertically above the queen
        {

            if(board[newRow][column].equals("Q"))
                placeTaken = true;
        }

        for(newRow = row + 1; newRow < board.length; newRow++) // vertically under the queen
        {
            if(newRow >= 8 || column >= 8)
                break;

            if(board[newRow][column].equals("Q"))
                placeTaken = true;
        }

        for(newCol = column + 1; newCol < board.length -1; newCol++) // horizontally to the right of the queen
        {


            if(board[row][newCol].equals("Q"))
                placeTaken = true;
        }

         for(newCol = column - 1; newCol >= 0; newCol--) // horizontally to the left of the queen
        {


            if(board[row][newCol].equals("Q"))
                placeTaken = true;
        }

        newCol = column + 1;
        for(newRow = row - 1; newRow >= 0; newRow--) // Queen's upper right diagonal
        {
            if(newRow >= 8 || newCol >= 8)
                break;

            if(board[newRow][newCol].equals("Q"))
                placeTaken = true;

            newCol++;    
        }

        newCol = column + 1;
        for(newRow = row + 1; newRow < board.length - 1; newRow++) // Queen's lower right diagonal
        {
            if(newRow >= 8 || newCol >= 8)
                break;

            if(board[newRow][newCol].equals("Q"))
                placeTaken = true;

            newCol++;    
        }

        newCol = column - 1;
        for(newRow = row - 1; newRow >= 0; newRow--) // Queen's upper left diagonal
        {
            if(newRow < 0  || newCol < 0)   
                break;

            if(board[newRow][newCol].equals("Q"))
                placeTaken = true;

            newCol--;    
        }

        newCol = column - 1;
        for(newRow = row + 1; newRow < board.length; newRow++) // Queen's lower left diagonal
        {
            if(newRow < 0  || newCol < 0)
                break;

            if(board[newRow][newCol].equals("Q"))
                placeTaken = true;

            newCol--;    
        }

        return placeTaken;
    } 

   public static void print(String[][] array)
   {
       for(int i = 0; i < array.length; i++)
       {
           for(int j = 0; j < array.length; j++)
           {
                    System.out.println(array[i][j] + "   ");
           }


       }
    }



}  

1 个答案:

答案 0 :(得分:3)

它为您提供了五种不同的解决方案。如果您只想要一个,那么在打印一个变量后,将boolean foundSolution变量设置为true,然后在打印后续解决方案之前检查此变量。或者更好的是,在继续进行任何进一步的计算之前检查它,因为一旦你找到了一个你不需要继续进行的计算。

编辑:实际上,问题中粘贴的输出是不完整的。完整输出在这里:http://ideone.com/WeU4M显示临时解决方案。最后有四个完整的董事会副本。如果您按照我上面的建议操作,可以防止最后打印的副本。