def number_solutions(copy_s, row, col): num_solutions = 0 if row == 8 and col == 8: return num_solutions + 1 if col == 9: row = row + 1 col = 0 if copy_s[row][col] == 0: present = Solver.test_cell(copy_s, row, col) if 0 not in present: return 0 while 0 in present: copy_s[row][col] = present.index(0) present[present.index(0)] = 1 num_solutions += number_solutions(copy_s, row, col + 1) copy_s[row][col] = 0 return num_solutions num_solutions += number_solutions(copy_s, row, col + 1) return num_solutions
def fill_sudoku(s, row, col): if row == 8 and col == 8: present = Solver.test_cell(s, row, col) s[row][col] = present.index(0) return True if col == 9: row = row + 1 col = 0 sequence = [1, 2, 3, 4, 5, 6, 7, 8, 9] random.shuffle(sequence) for i in range(9): s[row][col] = sequence[i] if valid_cell(s, row, col): if fill_sudoku(s, row, col + 1): return True s[row][col] = 0 return False