Skip to content

Commit da8feca

Browse files
committed
Complete Backtracking Class
1 parent 903abaf commit da8feca

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

18-Backtracking/Backtracking.java

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,81 @@ public static boolean nQueensSol1(char board[][], int row) {
134134
return false;
135135
}
136136

137+
public static int gridWays(int i, int j, int n, int m) {
138+
// Base case
139+
if (i == n - 1 && j == m - 1) { // condition for last cell
140+
return 1; // 1 way
141+
} else if (i == n || j == m) { // boundary cross condition
142+
return 0; // 0 way
143+
}
144+
int w1 = gridWays(i + 1, j, n, m);
145+
int w2 = gridWays(i, j + 1, n, m);
146+
return w1 + w2;
147+
}
148+
149+
public static boolean sudokuSolver(int sudoku[][], int row, int col) {
150+
// base case
151+
if (row == 9) {
152+
return true;
153+
}
154+
//recursion
155+
int nextRow = row, nextCol = col + 1;
156+
if (col + 1 == 9) {
157+
nextRow = row + 1;
158+
nextCol = 0;
159+
}
160+
if (sudoku[row][col] != 0) { // already placed
161+
return sudokuSolver(sudoku, nextRow, nextCol);
162+
}
163+
for (int digit = 1; digit <= 9; digit++) { // choice digit 1 to 9
164+
if (isSafeSudoku(sudoku, row, col, digit)) {
165+
sudoku[row][col] = digit;
166+
if (sudokuSolver(sudoku, nextRow, nextCol)) { // solution exists
167+
return true;
168+
}
169+
sudoku[row][col] = 0; // backtracking
170+
}
171+
}
172+
return false;
173+
}
174+
175+
public static boolean isSafeSudoku(int sudoku[][], int row, int col, int digit) {
176+
//column
177+
for (int i = 0; i <= 8; i++) {
178+
if (sudoku[i][col] == digit) {
179+
return false;
180+
}
181+
}
182+
//row
183+
for (int j = 0; j <= 8; j++) {
184+
if (sudoku[row][j] == digit) {
185+
return false;
186+
}
187+
}
188+
// grid (3 x 3)
189+
int sr = (row / 3) * 3; // 1,2,3
190+
int sc = (col / 3) * 3; // 1,2,3
191+
for (int i = sr; i < sr + 3; i++) {
192+
for (int j = sc; j < sc + 3; j++) {
193+
if (sudoku[i][j] == digit) {
194+
return false;
195+
}
196+
}
197+
}
198+
return true;
199+
}
200+
201+
public static void printSudoku(int sudoku[][]) {
202+
for (int i = 0; i < sudoku.length; i++) {
203+
for (int j = 0; j < sudoku.length; j++) {
204+
System.out.print(sudoku[i][j] + " ");
205+
}
206+
System.out.println();
207+
}
208+
}
209+
210+
// ------------------------------------------------------------------------------------------------
211+
137212
public static void main(String[] args) {
138213
// Type of Backtracking --------------------------------
139214
// 1. Decision
@@ -183,6 +258,32 @@ public static void main(String[] args) {
183258
} else {
184259
System.out.println("Solution is not possible");
185260
}
261+
System.out.println();
186262

263+
// Grid Ways --------------------------------
264+
// Find number of ways to reach from (0,0) to (N-1,M-1) in a NxM grid. (Allowed moves - right and down) ---
265+
int row = 3, col = 3;
266+
System.out.println("Number of ways : " + gridWays(0, 0, row, col));
267+
System.out.println();
268+
269+
// Sudoku --------------------------------
270+
// Write a function to complete a Sudoku. ---
271+
int sudoku[][] = {
272+
{ 0, 0, 8, 0, 0, 0, 0, 0, 0 },
273+
{ 4, 9, 0, 1, 5, 7, 0, 0, 2 },
274+
{ 0, 0, 3, 0, 0, 4, 1, 9, 0 },
275+
{ 1, 8, 5, 0, 6, 0, 0, 2, 0 },
276+
{ 0, 0, 0, 0, 2, 0, 0, 6, 0 },
277+
{ 9, 6, 0, 4, 0, 5, 3, 0, 0 },
278+
{ 0, 3, 0, 0, 7, 2, 0, 0, 4 },
279+
{ 0, 4, 9, 0, 3, 0, 0, 5, 7 },
280+
{ 8, 2, 7, 0, 0, 9, 0, 1, 3 }
281+
};
282+
if (sudokuSolver(sudoku, 0, 0)) {
283+
System.out.println("----------Solution exists----------");
284+
printSudoku(sudoku);
285+
} else {
286+
System.out.println("----------Solution not exists----------");
287+
}
187288
}
188289
}

0 commit comments

Comments
 (0)