mercredi 15 avril 2020

How is this code is reachable in this recursion function?

I'm doing a sudoku solver and found this code online, which used a snimilar method to what I was doing, but I don't understand how grid[y][x] = 0 is able to be reached in this code since it calls solve(grid)in the line before.

I've noticed I get the same output if I put a else statement before it (and as well if it has on less indent)

def solve(grid):
    for y in range(9):
        for x in range(9):
            if grid[y][x] == 0:
                for n in range(1, 10):
                    if possible(y, x, n, grid):
                        grid[y][x] = n
                        solve(grid)
                        grid[y][x] = 0  # How is the program able to reach this code if it calls solve(grid) before it's able to be reached? 

                return

    print(np.matrix(grid))

Aucun commentaire:

Enregistrer un commentaire