-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKKnightsMax.java
More file actions
96 lines (83 loc) · 2.49 KB
/
KKnightsMax.java
File metadata and controls
96 lines (83 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package BackTracking;
public class KKnightsMax {
static int maxKnights = -1;
static int number = 8;
private static boolean isSafe(char[][] board, int row, int col) {
int n = board.length;
int i, j;
// 2 Up 1 Right
i = row - 2;
j = col + 1;
if (i >= 0 && j < n && board[i][j] == 'K')
return false;
// 2 Up 1 Left
i = row - 2;
j = col - 1;
if (i >= 0 && j >= 0 && board[i][j] == 'K')
return false;
// 2 Down 1 Right
i = row + 2;
j = col + 1;
if (i < n && j < n && board[i][j] == 'K')
return false;
// 2 Down 1 Left
i = row + 2;
j = col - 1;
if (i < n && j >= 0 && board[i][j] == 'K')
return false;
// 2 Right 1 Down
i = row + 1;
j = col + 2;
if (i < n && j < n && board[i][j] == 'K')
return false;
// 2 Right 1 Up
i = row - 1;
j = col + 2;
if (i >= 0 && j < n && board[i][j] == 'K')
return false;
// 2 Left 1 Down
i = row + 1;
j = col - 2;
if (i < n && j >= 0 && board[i][j] == 'K')
return false;
// 2 Left 1 Up
i = row - 1;
j = col - 2;
return i < 0 || j < 0 || board[i][j] != 'K';
}
private static void nKnight(char[][] board, int row, int col, int num) {
int n = board.length;
// Base Case
if (row == n) {
maxKnights = Math.max(maxKnights, num);
return;
} else if (isSafe(board, row, col)) {
board[row][col] = 'K';
if (col != n - 1)
nKnight(board, row, col + 1, num + 1);
else
nKnight(board, row + 1, 0, num + 1);
board[row][col] = 'X'; // backtracking
}
// Always we will run this. Select / Don't Select.
if (col != n - 1)
nKnight(board, row, col + 1, num);
else
nKnight(board, row + 1, 0, num);
}
/*
* Find the maximum number of knights that can be placed in the board of nxn
* such that they don't attack each other.
*/
public static void main(String[] args) {
int n = 4;
char[][] board = new char[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
board[i][j] = 'X';
}
}
nKnight(board, 0, 0, 0);
System.out.println(maxKnights);
}
}