0

I have a problem with a simple code that tries to solve a sudoku. I skip here the details about the methods that I used to solve the sudoku. The only thing I think is important to know at the moment is: when the algorithm gets stucked (because, for example, we cannot find no more numbers in the sudoku in a simple way) I call a function that searches a box, among the empty ones, that can contain only two possible numbers. Then the function picks up the greatest number between the two candidates and substitutes it in the empty box. Then I ask the code to try to solve the sudoku with this new number. Obviously, I'd like to save a backup copy of the sudoku matrix before substituting my guess. Here comes the problem... When I modify the sudoku matrix also the backup copy gets modified. At the moment I didn't find any solution. To be more clear I report hereunder a simplified version of the code that presents the problem:

from pylab import *

data= loadtxt('bla.txt')

matrix= []


for i in range(0,len(data)):
    raw=[]
    for j in range(0,len(data)):
        if i==0 and j==0:
            raw.append([data[i][j],1,3])
        else:
            raw.append([data[i][j],1,2,3,4,5,6,7,8,9])
    matrix.append(raw)

def cancella(i,j,matrix):
    numero= matrix[i][j][0]
    for k in range(0,len(matrix)): 
        if matrix[i][k][0]==0. and numero in matrix[i][k]:
            matrix[i][k].pop(matrix[i][k].index(numero))
        if matrix[k][j][0]==0. and numero in matrix[k][j]:
            matrix[k][j].pop(matrix[k][j].index(numero))
    return matrix

candidati=[]
indice1=[]
indice2=[]

backup=[]
matrix=asarray(matrix)
sudoku=matrix.copy()

matrix=matrix.tolist()
sudoku=sudoku.tolist()
backup.append(sudoku)


for i in range(0,len(matrix)):
    bandiera= False
    for j in range(0,len(matrix)):
        if matrix[i][j][0]==0. and len(matrix[i][j])==3.:
            candidati.append(matrix[i][j][1])
            candidati.append(matrix[i][j][2])
            indice1.append(i)
            indice2.append(j)
            matrix[i][j][0]= matrix[i][j][2]
            matrix= cancella(i,j,matrix)
            bandiera= True
            break
    if bandiera:
        break

print backup, matrix

Briefly, the routine "cancella" is called everytime a new number is inserted in a box. This function erases the number from the box's column and raw pockets. First raws of the code load the .txt file in which the sudoku matrix is stored. I deliberately modified here the first matrix's item, namely matrix[0][0], so as to have a box with only two possible candidates, which is the situation that I was referring to at the beginning of my message. If you try to run the code you will see that backup and matrix have no difference, so the modifications in matrix have been recorded in backup too, which is NOT what I'd like. I'd like to preserve a backup copy of matrix and to modify matrix without modifying its backup copy.

I'd like to ask whether anybody knows how to solve this problem. Thanks to all for your kind attention. Andrea

P.s. The bla.txt file contains the sudoku matrix in the following form. For example:

0 1 0 1 0 0 0 4 0
1 0 0 0 0 0 0 8 0
4 0 0 0 0 0 0 3 0
0 0 0 4 9 7 5 0 3
0 3 7 0 5 0 2 0 4
0 0 0 2 0 0 0 7 0
0 6 0 0 7 2 0 0 0
5 0 3 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 

where 0 means that the box is empty. Thanks a lot for your help!

0 Answers0