Created
June 14, 2020 19:01
-
-
Save EonYang/ed650b9df51dc1eb32b3b55a76a44909 to your computer and use it in GitHub Desktop.
Soduku Solver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const solveSudoku = (grid, level) => { | |
let tries = 0; | |
// solve : (number[][], number, number)=> boolean | |
const solve = (grid, r, c) => { | |
if (c === level * level) { | |
r++; | |
c = 0; | |
if (r === level * level) return true; | |
} | |
if (grid[r][c] !== '.') return solve(grid, r, c + 1); | |
let solved = false; | |
const candidates = getPossibleNumsForACell(grid, r, c, level); | |
for (let n of candidates) { | |
tries++; | |
grid[r][c] = n; | |
solved = solve(grid, r, c + 1); | |
if (solved) break; | |
grid[r][c] = '.'; | |
} | |
return solved; | |
}; | |
solve(grid, 0, 0); | |
console.log(tries); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment