-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
105 lines (92 loc) · 2.87 KB
/
Copy pathTest.java
File metadata and controls
105 lines (92 loc) · 2.87 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
97
98
99
100
101
102
103
104
105
import java.util.*;
class Test {
public static void main(String[] args) {
// int[] arr = { 1, 2, 3 };
// Total permutations is N! i.e 3x2x1 = 6
// printPermutation(arr, 0, new boolean[arr.length], new ArrayList<Integer>());
int n = 4;
char[][] arr = new char[n][n];
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
arr[i][j] = '.';
}
}
nQueens(arr, n);
}
public static void printPermutation(int[] arr, int pos, boolean[] selected, ArrayList<Integer> ans) {
if (pos == arr.length) {
System.out.println(ans);
return;
}
for (int i = 0; i < arr.length; i++) {
if (selected[i] == false) {
selected[i] = true;
ans.add(arr[i]);
printPermutation(arr, pos + 1, selected, ans);
selected[i] = false;
ans.remove(ans.size() - 1); // Backtrack to remove the current element from the array and list.
}
}
}
// Recursion track has N length
public static void tss(int[] arr, int i, int target, ArrayList<Integer> ans) {
if (target < 0)
return;
if (i == arr.length) {
System.out.println(ans);
return;
}
// Accept the ith element
ans.add(arr[i]);
tss(arr, i + 1, target - arr[i], ans);
ans.remove(ans.size() - 1);
// Reject the ith element
tss(arr, i + 1, target, ans);
}
public static void print(char[][] arr) {
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static boolean isSafe(char[][] arr, int row, int col) {
// Check the column
for (int i = 0; i < row; i++) {
if (arr[i][col] == 'Q')
return false;
}
// Check the upper left diagonal
int i = row - 1, j = col - 1;
while (i >= 0 && j >= 0) {
if (arr[i][j] == 'Q')
return false;
i--;
j--;
}
// Check the upper right diagonal
i = row - 1;
j = col + 1;
while (i >= 0 && j < arr[0].length) {
if (arr[i][j] == 'Q')
return false;
i--;
j++;
}
return true;
}
public static void nQueens(char[][] arr, int row) {
if (row == arr.length) {
print(arr);
return;
}
for (int col = 0; col < arr[0].length; col++) {
if (isSafe(arr, row, col)) {
arr[row][col] = 'Q';
nQueens(arr, row + 1);
arr[row][col] = '.';
}
}
}
}