apps/sudoku-solver/controllers/sudoku-solver.js
// for Sudoku solution (non-duplication across rows, columns, and boxes)
for Sudoku solution (non-duplication across rows, columns, and boxes)
for Sudoku solution ( non - duplication across rows columns and boxes )
solveSudoku(grid) {
    this._recursions++;
    if (this._recursions > 50000) {
      return false;
    }
    let [row, col] = this.getUnassignedLocation(grid)
    if (row === 10 || col === 10) {
      return true;
    }
    for (let num = 1; num <= 9; num++) {
      if (this.isSafe(grid, row, col, num)) {
        grid[row][col] = num;
        if (this.solveSudoku(grid)) {
          return true;
        }
        grid[row][col] = 0;
      }
    }
    return false;
  }
solveSudoku ( grid ) { this . _recursions ++ ; if ( this . _recursions > 50000 ) { return false ; } let [ row , col ] = this . getUnassignedLocation ( grid ) if ( row === 10 || col === 10 ) { return true ; } for ( let num = 1 ; num <= 9 ; num ++ ) { if ( this . isSafe ( grid , row , col , num ) ) { grid [ row ] [ col ] = num ; if ( this . solveSudoku ( grid ) ) { return true ; } grid [ row ] [ col ] = 0 ; } } return false ; }
for Sudoku solution (non-duplication across rows, columns, and boxes)
for Sudoku solution ( non - duplication across rows columns and boxes )
// Tested the recursion count for the "hardest sudoku" puzzle // Puzzle was fetched from https://www.conceptispuzzles.com/index.aspx?uri=info/article/424 // Recursion count was 49559 - rounded to 50000 for nice number + a bit of buffer. // Anything exceeding this should be an unsolvable puzzle. // If the sudoku grid has been filled, we are done // Consider digits 1 to 9 // If placing the current number in the current // unassigned location is valid, go ahead // Make tentative assignment // Do the same thing again recursively. If we go // through all of the recursions, and in the end // return true, then all of our number placements // on the sudoku grid are valid and we have fully // solved it // As we were not able to validly go through all // of the recursions, we must have an invalid number // placement somewhere. Lets go back and try a // different number for this particular unassigned location // If we have gone through all possible numbers for the current unassigned // location, then we probably assigned a bad number early. Lets backtrack // and try a different number for the previous unassigned locations.