-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBacktracking.java
More file actions
108 lines (87 loc) · 2.63 KB
/
Backtracking.java
File metadata and controls
108 lines (87 loc) · 2.63 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
106
107
108
public class Backtracking {
public static void changeArr(int arr[], int i, int val){
// base case
if(i == arr.length){
printArr(arr);
return;
}
// Recursion
arr[i] = val;
changeArr(arr, i+1, val+1); // Funct call step
arr[i] = arr[i]-2; // backtraking step
}
// Find Subset of Strings
public static void findSubsets(String str, String ans, int i){
if(i == str.length()){
if(ans.length() == 0){
System.out.print("null");
}
System.out.println(ans);
return;
}
// Yes Choice
findSubsets(str, ans+str.charAt(i), i+1);
// No Choice
findSubsets(str, ans, i+1);
}
//Find Permutations
public static void findPermutation(String str, String ans){
if(str.length() == 0){
System.out.println(ans);
return;
}
for(int i=0; i<str.length(); i++){
char curr = str.charAt(i);
String NewStr = str.substring(0, i) + str.substring(i+1);
findPermutation(NewStr, ans+curr);
}
}
//nQueens :
public static void nQueens(char board[][], int row){
// Base
if(row == board.length){
printBoard(board);
return;
}
// Column loop
for(int j=0; j<board.length; j++){
board[row][j] = 'Q';
nQueens(board, row+1);
board[row][j] = '.';
}
}
public static void printBoard(char board[][]){
System.out.println("--------------Chess Board--------------");
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();
}
}
public static void printArr(int arr[]){
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
System.out.println();
}
public static void main(String[] args) {
// int arr[] = new int[5];
// changeArr(arr, 0, 1);
// printArr(arr);
// String str = "ab";
// findSubsets(str, "", 0);
// https://chatgpt.com/s/t_68dbc5243f008191a9e0996e0821e483
// String str = "ab";
// findPermutation(str, "");
// https://chatgpt.com/s/t_68de09b24c1881918e9f23b0ceede553
int n = 2;
char board[][] = new char[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
board[i][j] = '.';
}
}
nQueens(board, 0);
}
}