I'm trying to write a simple sudoku solver in python. The basic concept is that the sudoku puzzle is partially filled in and the unsolved cells are indicated by zeros. Any cell denoted by a zero can be solved at any stage of the puzzle. So if the first cell is 0, then it means the values in that row, column and 3x3 subgrid ensure that there can only be one possible value for that cell. Here is my code, I seem to be stuck because the output displays more than one possibility. Is my algorithm wrong?
def solveOne (array, posR, posC):
possible = ['1','2','3','4','5','6','7','8','9']
for col in range (9):
if array[posR][col] in possible:
possible.remove(array[posR][col])
for row in range (9):
if array[row][posC] in possible:
possible.remove(array[row][posC])
for row in range(posR, posR+3):
for col in range (posC, posC+3):
if array[row::][col::] in possible:
possible.remove(array[row][col])
print (possible)
return possible
grid = [["" for _ in range(9)] for _ in range(9)] #define a 9x9 2-dimensional list
for row in range(9):
aLine = input() #prompt user to enter one line of characters
for col in range(9):
grid[row][col] = aLine[col:col+1] #assign every character in a line to a position in the 2-D array
for row in range(9):
for col in range (9):
if grid[row][col] == '0':
r = row
c = col
newV = solveOne (grid,r,c)
grid[row][col] = newV
print()
for i in range (9):
for k in range(9):
print(grid[i][k], end = "")
print()