I am trying to solve the puzzle of placing k non-attacking bishops on an 8x8 chessboard. http://mathworld.wolfram.com/BishopsProblem.html What i do is try to place the rook on a square, if However I have some problem in my backtrack method, because I only print the first found solution. The methods finds solutions only for GOAL <= 12, and I know there are is a solution for if that might give any clue...
static final char EMPTY = '*';
static final char BISHOP = 'B';
static final int GOAL = 10;
static void solve(char[][] board, int row, int col, int placed) {
if (placed == GOAL) {
printSolution(board);
return;
}
if (col == board.length) {
col = 0;
row++;
if (row == board.length) {
return;
}
}
if (isSafe(board, row, col)) {
board[row][col] = BISHOP; // PLACE BISHOP
solve(board, row, col + 1, placed + 1);
} else {
solve(board, row, col + 1, placed);
return;
}
board[row][col] = EMPTY; // BACKTRACK
}
here is the output when calling solve(board, 0, 0, 0)
B B B B B B B B
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * *
* * * B B * * *
* * * * * * * *
* * * * * * * *
