-
Notifications
You must be signed in to change notification settings - Fork 0
/
37-sudokuSolver.js
71 lines (54 loc) · 1.35 KB
/
37-sudokuSolver.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
62
63
64
65
66
67
68
69
70
71
/**
* @param {character[][]} board
* @return {void} Do not return anything, modify board in-place instead.
*/
var solveSudoku = function(board) {
function possible(y,x,n) {
for (let i = 0; i < 9; i++){
if (board[y][i] === n.toString()) {
return false
}
}
for (let i = 0; i < 9; i++){
if (board[i][x] === n.toString()) {
return false
}
}
const x0 = Math.floor(x / 3) * 3
const y0 = Math.floor(y/3) * 3
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[y0+i][x0+j] === n.toString()) {
return false
}
}
}
return true
}
for (let i = 0; i < 9; i++){
for (let j = 0; j < 9; j++) {
if (board[i][j] === ".") {
for (let k = 1; k < 10; k++) {
if (possible(i, j, k)) {
board[i][j] = k.toString()
solveSudoku(board)
board[i][j] = "."
}
}
}
return board
}
}
}
const board = [
[".", ".", "9", "7", "4", "8", ".", ".", "."],
["7", ".", ".", ".", ".", ".", ".", ".", "."],
[".", "2", ".", "1", ".", "9", ".", ".", "."],
[".", ".", "7", ".", ".", ".", "2", "4", "."],
[".", "6", "4", ".", "1", ".", "5", "9", "."],
[".", "9", "8", ".", ".", ".", "3", ".", "."],
[".", ".", ".", "8", ".", "3", ".", "2", "."],
[".", ".", ".", ".", ".", ".", ".", ".", "6"],
[".", ".", ".", "2", "7", "5", "9", ".", "."],
];
console.log(solveSudoku(board))