sudoku backtracking python

Viewed 50
def solveSudoku(grid):
  N = 9
  loc = Unassigned(grid)

  if (not loc):
    return True

  for i in loc:
    row, col = i
    for num in range(1,N+1):
      if checkSafety(grid, row, col, num):
        grid[row][col] = num
        if(solveSudoku(grid)):
          print(grid)
          return True
        grid[row][col] = 0      

  return False

I'm trying to write a code to solve sudoku via backtracking. However, the above code takes too long to run (3mins in and its still not done). I believe this is because the output of my Unassigned(grid) function is a list of indices. It is a function to get the list of indices of locations whose values are 0. But that is supposed to be the desired and required output of that function.

0 Answers
Related