forked from singhsanket143/FrontendDSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.js
61 lines (58 loc) · 1.63 KB
/
sudoku.js
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
let grid = [
[3, '.', 6, 5, '.', 8, 4, '.', '.'],
[5, 2, '.', '.', '.', '.', '.', '.', '.'],
['.', 8, 7, '.','.','.','.',3, 1],
['.','.',3, '.','1','.','.',8,'.'],
[9, '.','.', 8, 6, 3, '.','.',5],
['.', 5, '.','.',9,'.',6,'.','.'],
[1,3,'.','.','.','.',2,5,'.'],
['.','.','.','.','.','.','.',7, 4],
['.','.',5, 2, '.', 6, 3, '.','.']
];
function canWePlace(row, col, num) {
// check the col
for(let i = 0; i < 9; i++) {
if(grid[i][col] == num) return false;
}
// cehck the row
for(let i = 0; i < 9; i++) {
if(grid[row][i] == num) return false;
}
// check the big cell
let R = Math.floor(row/3);
let C = Math.floor(col/3);
for(let i = R*3; i < (R*3) + 3; i++) {
for(let j = C*3; j < (C*3) + 3; j++) {
if(grid[i][j] == num) return false;
}
}
return true;
}
function f(r, c) {
// if we have exhauster the column, go to the next row
if(c == 9) {
f(r+1, 0);
return;
}
if(r == 9) {
// we found a valid sudoku solution
console.log(grid);
console.log("*****");
return;
}
if(grid[r][c] == '.') {
// if the current cell is empty, we will try to fill it
for(let num = 1; num <= 9; num++) {
if(canWePlace(r, c, num)) {
// if it is safe to place the number we place it
grid[r][c] = num; // set the state
f(r, c+1);
grid[r][c] = '.'; // reset the state
}
}
} else {
// if the current cell is already filled by a number
f(r, c+1);
}
}
f(0, 0);