i am getting 51 output instead of 36 if i take a 3x3 board .
can anyone tell me why?
my code.
package Section_10_Backtracking;
public class N_Knights {
static int count = 0;
public static void main(String[] args) {
knight_not_kill(new boolean[3][3], 0, 0, 0, 3, "");
}
public static void knight_not_kill(boolean[][] board, int row, int col, int qsf, int tq, String ans) {
if (qsf == tq) {
count++;
System.out.println(count + ". " + ans);
return;
}
if (col == board[0].length) {
row++;
col = 0;
}
if (row == board.length) {
return;
}
if (is_safe(board, row, col)) {
board[row][col] = true;
knight_not_kill(board, row, col + 1, qsf + 1, tq, ans + row + col + " ");
board[row][col] = false;
}
knight_not_kill(board, row, col + 1, qsf, tq, ans);
}
public static boolean is_safe(boolean[][] board, int row, int col) {
int[] rarray = new int[] { -1, -2, -2, -1 };
int[] carray = new int[] { -2, -1, 1, 2 };
for (int i = 0; i < 4; i++) {
int r = row + rarray[i];
int c = col + rarray[i];
if (r >= 0 && c >= 0 && r < board.length && c < board[0].length) {
if (board[r][c] == true) {
return false;
}
}
}
return true;
}
}