2010-12-19 46 views
-1

輸出主題:8皇后,當我打印板給我五份,我只想要一個?

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

雖然您的輸出很有幫助,但我們無法在不看到它的情況下診斷您的代碼。 – 2010-12-19 17:04:28

+0

你希望我們做什麼?爲你調試它!? – synepis 2010-12-19 17:05:06

+0

@sklitzz:如果你可以幫忙.. – 2010-12-19 17:10:34

回答

3

它給你五個不同的解決方案。如果你只需要一個,那麼一旦你打印了一個,然後在打印後續解決方案之前檢查這個變量,就會傳遞一個boolean foundSolution變量,你設置爲true。或者更好的是,在繼續進行任何進一步的計算之前檢查它,因爲一旦你發現一個你不需要繼續進一步計算。

編輯:其實,粘貼在問題的輸出是不完整的。完整的輸出在這裏:http://ideone.com/WeU4M它顯示了臨時解決方案。最後有四份全版。如果你按照我上面的建議,你可以防止最後打印的副本。