Skip to content

Commit 04d5412

Browse files
authored
Merge pull request #69 from TORRYNN/BackTracking
Back tracking
2 parents e6aa4f1 + fc05a4f commit 04d5412

File tree

10 files changed

+235
-0
lines changed

10 files changed

+235
-0
lines changed

BackTracking/FindPermutation.class

1.3 KB
Binary file not shown.

BackTracking/FindPermutation.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class FindPermutation {
2+
public static void Perm(String str, String ans) {
3+
// Base Case
4+
if (str.length() == 0) {
5+
System.out.println(ans);
6+
return;
7+
}
8+
9+
for (int i = 0; i < str.length(); i++) {
10+
11+
char current = str.charAt(i);
12+
// Deleting the current character from the string
13+
String newStr = str.substring(0, i) + str.substring(i + 1);
14+
Perm(newStr, ans + current);
15+
}
16+
}
17+
18+
public static void main(String args[]) {
19+
Perm("abc", "");
20+
21+
}
22+
}

BackTracking/GridWay.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class GridWay {
2+
public static void main(String args[]) {
3+
4+
}
5+
}

BackTracking/Nqueen.class

1.64 KB
Binary file not shown.

BackTracking/Nqueen.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
public class Nqueen {
2+
public static void nqueen(char board[][], int row) {
3+
if (row == board.length) {
4+
printBoard(board);
5+
return;
6+
}
7+
8+
for (int j = 0; j < board.length; j++) {
9+
if (isSafe(board, row, j)) {
10+
11+
board[row][j] = 'Q';
12+
nqueen(board, row + 1);
13+
board[row][j] = 'X';
14+
}
15+
}
16+
}
17+
18+
public static boolean isSafe(char board[][], int row, int col) {
19+
20+
// Vertical Up
21+
for (int i = row - 1; i >= 0; i--) {
22+
if (board[i][col] == 'Q') {
23+
return false;
24+
}
25+
}
26+
27+
// Diagonal left Up
28+
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
29+
if (board[i][j] == 'Q') {
30+
return false;
31+
}
32+
}
33+
34+
// Diagonal right up
35+
for (int i = row - 1, j = col + 1; i >= 0 && j < board.length; i--, j++) {
36+
if (board[i][j] == 'Q') {
37+
return false;
38+
}
39+
}
40+
41+
return true;
42+
}
43+
44+
public static void printBoard(char[][] board) {
45+
System.out.println("----------------");
46+
for (char row[] : board) {
47+
for (char element : row) {
48+
System.out.print(element + " ");
49+
}
50+
System.out.println();
51+
52+
}
53+
}
54+
55+
// Time Complexity:O(n!)
56+
57+
public static void main(String args[]) {
58+
int n = 4;
59+
char board[][] = new char[n][n];
60+
for (int i = 0; i < n; i++) {
61+
for (int j = 0; j < n; j++) {
62+
board[i][j] = 'X';
63+
}
64+
}
65+
66+
// Solve the N-Queens problem
67+
nqueen(board, 0);
68+
}
69+
}

BackTracking/Nqueencount.class

1.7 KB
Binary file not shown.

BackTracking/Nqueencount.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
public class Nqueencount {
2+
3+
public static boolean nqueen(char board[][], int row) {
4+
// Base Condition
5+
if (row == board.length) {
6+
7+
return true;
8+
}
9+
10+
for (int col = 0; col < board.length; col++) {
11+
if (isSafe(board, row, col)) {
12+
board[row][col] = 'Q';
13+
if(nqueen(board, row + 1)){
14+
return true;
15+
}
16+
17+
board[row][col] = 'X';
18+
}
19+
}
20+
return false;
21+
}
22+
23+
public static boolean isSafe(char board[][], int row, int col) {
24+
// Vertical Up
25+
for (int i = row - 1; i >= 0; i--) {
26+
if (board[i][col] == 'Q') {
27+
return false;
28+
}
29+
30+
}
31+
32+
// Diagonal left Up
33+
for (int i = row - 1, j = col - 1; i >= 0 && j >= 0; i--, j--) {
34+
if (board[i][j] == 'Q') {
35+
return false;
36+
}
37+
38+
}
39+
40+
// Diagonal right up
41+
for (int i = row - 1, j = col + 1; i >= 0 && j < board.length; i--, j++) {
42+
if (board[i][j] == 'Q') {
43+
return false;
44+
}
45+
}
46+
return true;
47+
}
48+
49+
public static void printboard(char[][] board) {
50+
System.out.println("----------------");
51+
for (char row[] : board) {
52+
for (char element : row) {
53+
System.out.print(element + " ");
54+
}
55+
System.out.println();
56+
57+
}
58+
}
59+
60+
public static void main(String[] args) {
61+
int n = 4;
62+
char board[][] = new char[n][n];
63+
for (int i = 0; i < n; i++) {
64+
for (int j = 0; j < n; j++) {
65+
board[i][j] = 'X';
66+
}
67+
}
68+
69+
// Solve the N-Queens problem
70+
71+
if (nqueen(board, 0)) {
72+
System.out.println("Solution is possible :");
73+
printboard(board);
74+
}
75+
}
76+
}

BackTracking/Subset.class

1.78 KB
Binary file not shown.

BackTracking/Subset.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// abc
2+
// a,b,c,bc,ac,abc
3+
4+
public class Subset {
5+
// Using Brute Force Approach
6+
public static void subset1(String str) {
7+
int n = str.length();
8+
int totalSubsets = (int) Math.pow(2, n);
9+
for (int i = 0; i < totalSubsets; i++) {
10+
StringBuilder subset = new StringBuilder();
11+
12+
// Check each bit of the number to decide inclusion of characters
13+
for (int j = 0; j < n; j++) {
14+
if ((i & (1 << j)) != 0) {
15+
subset.append(str.charAt(j));
16+
}
17+
}
18+
System.out.println(subset.toString());
19+
}
20+
21+
}
22+
23+
// Using BackTracking
24+
public static void subset2(String str, String ans, int i) {
25+
// Base Case
26+
27+
if (i == str.length()) {
28+
if (ans.length() == 0) {
29+
System.out.println("null");
30+
} else {
31+
System.out.println(ans);
32+
}
33+
return;
34+
}
35+
// Yes Choice
36+
subset2(str, ans + str.charAt(i), i + 1);
37+
38+
// No Choice
39+
subset2(str, ans, i + 1);
40+
}
41+
42+
// Backtracking using Stringbuilder
43+
public static void subset3(String str, StringBuilder ans, int i) {
44+
if (i == str.length()) {
45+
if (ans.length() == 0) {
46+
System.out.println("null");
47+
} else {
48+
System.out.println(ans);
49+
}
50+
return;
51+
}
52+
subset3(str, new StringBuilder(ans).append(str.charAt(i)), i + 1);
53+
54+
subset3(str, ans, i + 1);
55+
}
56+
57+
public static void main(String[] args) {
58+
subset1("abc");
59+
StringBuilder ans = new StringBuilder();
60+
subset3("abcd", ans, 0);
61+
}
62+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
board

0 commit comments

Comments
 (0)