MCPcopy Index your code
hub / github.com/TheAlgorithms/JavaScript / solve

Method solve

Backtracking/Sudoku.js:41–57  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

39 }
40
41 solve() {
42 const [y, x] = this.findEmptyCell()
43
44 // checking if the board is complete
45 if (y === -1 && x === -1) return true
46
47 for (let val = 1; val < 10; val++) {
48 if (this.check([y, x], val)) {
49 this.board[y][x] = val
50 if (this.solve()) return true
51 // backtracking if the board cannot be solved using current configuration
52 this.board[y][x] = 0
53 }
54 }
55 // returning false the board cannot be solved using current configuration
56 return false
57 }
58
59 getSection(row, [start, end]) {
60 return this.board[row].slice(start, end)

Callers 3

NQueens.test.jsFile · 0.45
KnightTour.test.jsFile · 0.45
Sudoku.test.jsFile · 0.45

Calls 2

findEmptyCellMethod · 0.95
checkMethod · 0.95

Tested by

no test coverage detected