-1
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] + " ");
}
}
}
}
雖然您的輸出很有幫助,但我們無法在不看到它的情況下診斷您的代碼。 – 2010-12-19 17:04:28
你希望我們做什麼?爲你調試它!? – synepis 2010-12-19 17:05:06
@sklitzz:如果你可以幫忙.. – 2010-12-19 17:10:34