Skip to content

Commit 86cb600

Browse files
questions
1 parent 0d8f0c0 commit 86cb600

File tree

4 files changed

+263
-0
lines changed

4 files changed

+263
-0
lines changed
1000 KB
Binary file not shown.
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.kunal.backtracking;
2+
3+
public class NKnights {
4+
public static void main(String[] args) {
5+
int n = 4;
6+
boolean[][] board = new boolean[n][n];
7+
knight(board, 0, 0, 4);
8+
}
9+
10+
static void knight(boolean[][] board, int row, int col, int knights) {
11+
if (knights == 0) {
12+
display(board);
13+
System.out.println();
14+
return;
15+
}
16+
17+
if (row == board.length - 1 && col == board.length) {
18+
return;
19+
}
20+
21+
if (col == board.length) {
22+
knight(board, row + 1, 0, knights);
23+
return;
24+
}
25+
26+
if (isSafe(board, row, col)) {
27+
board[row][col] = true;
28+
knight(board, row, col + 1, knights - 1);
29+
board[row][col] = false;
30+
}
31+
32+
knight(board, row, col + 1, knights);
33+
}
34+
35+
private static boolean isSafe(boolean[][] board, int row, int col) {
36+
if (isValid(board, row - 2, col - 1)) {
37+
if (board[row - 2][col - 1]) {
38+
return false;
39+
}
40+
}
41+
42+
if (isValid(board, row - 1, col - 2)) {
43+
if (board[row - 1][col - 2]) {
44+
return false;
45+
}
46+
}
47+
48+
if (isValid(board, row - 2, col + 1)) {
49+
if (board[row - 2][col + 1]) {
50+
return false;
51+
}
52+
}
53+
54+
if (isValid(board, row - 1, col + 2)) {
55+
if (board[row - 1][col + 2]) {
56+
return false;
57+
}
58+
}
59+
60+
return true;
61+
}
62+
63+
// do not repeat yourself, hence created this function
64+
static boolean isValid(boolean[][] board, int row, int col) {
65+
if (row >= 0 && row < board.length && col >= 0 && col < board.length) {
66+
return true;
67+
}
68+
return false;
69+
}
70+
71+
private static void display(boolean[][] board) {
72+
for(boolean[] row : board) {
73+
for(boolean element : row) {
74+
if (element) {
75+
System.out.print("K ");
76+
} else {
77+
System.out.print("X ");
78+
}
79+
}
80+
System.out.println();
81+
}
82+
}
83+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.kunal.backtracking;
2+
3+
public class NQueens {
4+
public static void main(String[] args) {
5+
int n = 4;
6+
boolean[][] board = new boolean[n][n];
7+
System.out.println(queens(board, 0));
8+
}
9+
10+
static int queens(boolean[][] board, int row) {
11+
if (row == board.length) {
12+
display(board);
13+
System.out.println();
14+
return 1;
15+
}
16+
17+
int count = 0;
18+
19+
// placing the queen and checking for every row and col
20+
for (int col = 0; col < board.length; col++) {
21+
// place the queen if it is safe
22+
if(isSafe(board, row, col)) {
23+
board[row][col] = true;
24+
count += queens(board, row + 1);
25+
board[row][col] = false;
26+
}
27+
}
28+
29+
return count;
30+
}
31+
32+
private static boolean isSafe(boolean[][] board, int row, int col) {
33+
// check vertical row
34+
for (int i = 0; i < row; i++) {
35+
if (board[i][col]) {
36+
return false;
37+
}
38+
}
39+
40+
// diagonal left
41+
int maxLeft = Math.min(row, col);
42+
for (int i = 1; i <= maxLeft; i++) {
43+
if(board[row-i][col-i]) {
44+
return false;
45+
}
46+
}
47+
48+
// diagonal right
49+
int maxRight = Math.min(row, board.length - col - 1);
50+
for (int i = 1; i <= maxRight; i++) {
51+
if(board[row-i][col+i]) {
52+
return false;
53+
}
54+
}
55+
56+
return true;
57+
}
58+
59+
private static void display(boolean[][] board) {
60+
for(boolean[] row : board) {
61+
for(boolean element : row) {
62+
if (element) {
63+
System.out.print("Q ");
64+
} else {
65+
System.out.print("X ");
66+
}
67+
}
68+
System.out.println();
69+
}
70+
}
71+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package com.kunal.backtracking;
2+
3+
public class SudokuSolver {
4+
public static void main(String[] args) {
5+
int[][] board = new int[][]{
6+
{3, 0, 6, 5, 0, 8, 4, 0, 0},
7+
{5, 2, 0, 0, 0, 0, 0, 0, 0},
8+
{0, 8, 7, 0, 0, 0, 0, 3, 1},
9+
{0, 0, 3, 0, 1, 0, 0, 8, 0},
10+
{9, 0, 0, 8, 6, 3, 0, 0, 5},
11+
{0, 5, 0, 0, 9, 0, 6, 0, 0},
12+
{1, 3, 0, 0, 0, 0, 2, 5, 0},
13+
{0, 0, 0, 0, 0, 0, 0, 7, 4},
14+
{0, 0, 5, 2, 0, 6, 3, 0, 0}
15+
};
16+
17+
if (solve(board)) {
18+
display(board);
19+
} else {
20+
System.out.println("Cannot solve");
21+
}
22+
23+
}
24+
25+
static boolean solve(int[][] board) {
26+
int n = board.length;
27+
int row = -1;
28+
int col = -1;
29+
30+
boolean emptyLeft = true;
31+
32+
// this is how we are replacing the r,c from arguments
33+
for (int i = 0; i < n; i++) {
34+
for (int j = 0; j < n; j++) {
35+
if (board[i][j] == 0) {
36+
row = i;
37+
col = j;
38+
emptyLeft = false;
39+
break;
40+
}
41+
}
42+
// if you found some empty element in row, then break
43+
if (emptyLeft == false) {
44+
break;
45+
}
46+
}
47+
48+
if (emptyLeft == true) {
49+
return true;
50+
// soduko is solved
51+
}
52+
53+
// backtrack
54+
for (int number = 1; number <= 9; number++) {
55+
if (isSafe(board, row, col, number)) {
56+
board[row][col] = number;
57+
if (solve(board)) {
58+
// found the answer
59+
return true;
60+
} else {
61+
// backtrack
62+
board[row][col] = 0;
63+
}
64+
}
65+
}
66+
return false;
67+
}
68+
69+
private static void display(int[][] board) {
70+
for(int[] row : board) {
71+
for(int num : row) {
72+
System.out.print(num + " ");
73+
}
74+
System.out.println();
75+
}
76+
}
77+
78+
79+
static boolean isSafe(int[][] board, int row, int col, int num) {
80+
// check the row
81+
for (int i = 0; i < board.length; i++) {
82+
// check if the number is in the row
83+
if (board[row][col] == num) {
84+
return false;
85+
}
86+
}
87+
88+
// check the col
89+
for (int[] nums : board) {
90+
// check if the number is in the col
91+
if (nums[col] == num) {
92+
return false;
93+
}
94+
}
95+
96+
int sqrt = (int)(Math.sqrt(board.length));
97+
int rowStart = row - row % sqrt;
98+
int colStart = col - col % sqrt;
99+
100+
for (int r = rowStart; r < rowStart + sqrt; r++) {
101+
for (int c = colStart; c < colStart + sqrt; c++) {
102+
if (board[r][c] == num) {
103+
return false;
104+
}
105+
}
106+
}
107+
return true;
108+
}
109+
}

0 commit comments

Comments
 (0)