-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathProblem_3_Sudoku_Solver.java
43 lines (39 loc) · 1.32 KB
/
Problem_3_Sudoku_Solver.java
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
package Backtracking;
// Problem Statement: Sudoku Solver (hard)
// LeetCode Question: 37. Sudoku Solver
public class Problem_3_Sudoku_Solver {
public boolean solveSudoku (char[][] board) {
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
if (board[row][col] == '.') {
for (int num = 1; num <= 9; num++) {
if (isValid(board, row, col, (char) (num + '0'))) {
board[row][col] = (char) (num + '0');
if (solveSudoku(board)) {
return true;
} else {
board[row][col] = '.';
}
}
}
return false;
}
}
}
return true;
}
private static boolean isValid (char[][] board, int row, int col, char num) {
for (int i = 0; i < 9; i++) {
if (board[row][i] == num) {
return false;
}
if (board[i][col] == num) {
return false;
}
if (board[(row / 3) * 3 + i / 3][(col / 3) * 3 + i % 3] == num) {
return false;
}
}
return true;
}
}