2010-12-19 2 views
-1

AusgabeBetreff: 8 Königinnen, wenn ich das Brett drucke, gibt es mir fünf Kopien und ich will nur eins?

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 

-Code

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

Während Ihre Ausgabe hilfreich ist, können wir Ihren Code nicht diagnostizieren, ohne ihn zu sehen. –

+0

Was wollen Sie von uns? Debuggen Sie es für Sie !? – synepis

+0

@ sklitzzz: Wenn Sie helfen können, bitte .. –

Antwort

3

Es können Sie fünf verschiedene Lösungen geben. Wenn Sie nur eine möchten, übergeben Sie eine boolean foundSolution Variable, die Sie auf true setzen, nachdem Sie eine gedruckt haben, und überprüfen Sie dann diese Variable, bevor Sie nachfolgende Lösungen drucken. Oder besser noch, überprüfen Sie es, bevor Sie mit der weiteren Berechnung fortfahren. Sobald Sie eine solche gefunden haben, müssen Sie nicht weiter fortfahren.

EDIT: Eigentlich ist die Ausgabe in der Frage unvollständig eingefügt. Volle Ausgabe ist hier: http://ideone.com/WeU4M, die Weg vorläufige Lösungen zeigt. Es gibt vier Kopien der Vollpension am Ende. Wenn Sie dem folgen, was ich oben vorgeschlagen habe, können Sie verhindern, dass die doppelten Kopien am Ende gedruckt werden.

Verwandte Themen