Counter of recursion solutions

Asked

Viewed 510 times

0

Hi, I made a recursive Sudoku program, it’s working fine, but I’d like to add a solution counter, I’ve tried numerous code changes that would make it possible, but it didn’t work, finally I tried a solution using global variables as I would implement in C as a last resort, and also didn’t work, but I don’t know if I used this variable correctly, the python code returned me the following error:

File "~/CCR/UserFiles/Marcilio/Sudoku/sudoku.py", line 176, in Sudoku
    contador += 1
UnboundLocalError: local variable 'contador' referenced before assignment

follows the code, with the global variable, or if there is another way to solve and can suggest me thank you:

contador = 0

def Sudoku(Mat, lin, col):   
    lin, col = ProcuraCasas(Mat)    
    if lin == -1 and col == -1:
        if TestaMatrizPreenchida(Mat):
            contador += 1
            ImprimaMatriz(Mat)
            print("\t")
        else: print("Preenchimento incorreto")    
    else:
        for candidato in range (1, 10):
            if VerificaCandidato (Mat, lin, col, candidato) is True:
                Mat[lin][col] = candidato     
                Sudoku(Mat, lin, col)           
        Mat[lin][col] = 0 

1 answer

2


At the beginning of your function add: global contador

Browser other questions tagged

You are not signed in. Login or sign up in order to post.