Solutions counter in recursion

0
Hello, I did a recursive Sudoku program, it works fine, but I would like to add a solution counter, I have tested numerous code changes that would allow this, but it did not work, I finally tried a solution using global variables as I would implement in C as but it did not work, but I do not 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

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 
    
asked by anonymous 18.09.2017 / 02:27

1 answer

2

At the beginning of your function add: global contador

    
18.09.2017 / 03:18