Skip to content

Commit bb891df

Browse files
Added: Sodoko Solver In DP (#420)
* Added: Sodoko Solver In DP * added: space remove * Chnage code accoding to npm standards. * Change * Update: All Issue Fix * Update SudokuSolver.js Co-authored-by: vinayak <itssvinayak@gmail.com>
1 parent 55760a8 commit bb891df

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
const _board = [
2+
['.', '9', '.', '.', '4', '2', '1', '3', '6'],
3+
['.', '.', '.', '9', '6', '.', '4', '8', '5'],
4+
['.', '.', '.', '5', '8', '1', '.', '.', '.'],
5+
['.', '.', '4', '.', '.', '.', '.', '.', '.'],
6+
['5', '1', '7', '2', '.', '.', '9', '.', '.'],
7+
['6', '.', '2', '.', '.', '.', '3', '7', '.'],
8+
['1', '.', '.', '8', '.', '4', '.', '2', '.'],
9+
['7', '.', '6', '.', '.', '.', '8', '1', '.'],
10+
['3', '.', '.', '.', '9', '.', '.', '.', '.']
11+
]
12+
13+
const isValid = (board, row, col, k) => {
14+
for (let i = 0; i < 9; i++) {
15+
const m = 3 * Math.floor(row / 3) + Math.floor(i / 3)
16+
const n = 3 * Math.floor(col / 3) + i % 3
17+
if (board[row][i] === k || board[i][col] === k || board[m][n] === k) {
18+
return false
19+
}
20+
}
21+
return true
22+
}
23+
24+
const sodokoSolver = (data) => {
25+
for (let i = 0; i < 9; i++) {
26+
for (let j = 0; j < 9; j++) {
27+
if (data[i][j] === '.') {
28+
for (let k = 1; k <= 9; k++) {
29+
if (isValid(data, i, j, k)) {
30+
data[i][j] = `${k}`
31+
if (sodokoSolver(data)) {
32+
return true
33+
} else {
34+
data[i][j] = '.'
35+
}
36+
}
37+
}
38+
return false
39+
}
40+
}
41+
}
42+
return true
43+
}
44+
45+
// testing
46+
(() => {
47+
if (sodokoSolver(_board)) {
48+
console.log(_board)
49+
}
50+
})()

0 commit comments

Comments
 (0)