-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0037_Sudoku_Solver.java
73 lines (62 loc) · 2.38 KB
/
0037_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
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
/*
* 37. Sudoku Solver
* Problem Link: https://leetcode.com/problems/sudoku-solver/
* Difficulty: Hard
*
* Solution Created by: Muhammad Khuzaima Umair
* LeetCode : https://leetcode.com/mkhuzaima/
* Github : https://github.com/mkhuzaima
* LinkedIn : https://www.linkedin.com/in/mkhuzaima/
*/
class Solution {
// Keep track of whether the sudoku puzzle is completed
boolean completed = false;
public void solveSudoku(char[][] board) {
// Iterate through each cell in the board
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
// Skip cells that already have a value
if (board[row][col] != '.') {
continue;
}
// Check all possible values for the current cell
for (char val = '1'; val <= '9'; val++) {
if (isValid(board, row, col, val)) {
// If the value is valid, set it in the board and recursively call solveSudoku
board[row][col] = val;
solveSudoku(board);
// If the puzzle is completed, return
if (completed) {
return;
}
// If the puzzle is not completed, unset the current cell and try the next value
board[row][col] = '.';
}
}
// If no value is valid for the current cell, return to the previous cell
return;
}
}
// If all cells have been filled, the puzzle is completed
completed = true;
}
private boolean isValid(char [][] board, int row, int col, char val) {
// Check if the value is unique in the current row and column
for (int i = 0; i < 9; i++) {
if (board[row][i] == val || board[i][col] == val) {
return false;
}
}
// Check if the value is unique in the current 3x3 grid
int rowStart = row / 3 * 3;
int colStart = col / 3 * 3;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
if (board[rowStart+i][colStart+j] == val) {
return false;
}
}
}
return true;
}
}