python - Passing matrix in recursive sudoku solver -
So I wrote the brutal force Sudoku Solver in a time ago. I thought it would be well in Python, so I can only translate my argument (a function can be held legally on (X, Y) and one that finds all solutions repeatedly.) Everything else (mostly I / O) I was able to replace with one-liners (Hurray for Python).
The problem is, the code does not work. "Sud" is a 9x9 matrix (in the beginning) contains unsolved sudoku, the unknown number is represented by zero.
The program does not seem to change the values stored in "sud", so I'm guessing this problem in which my metrics are defined? In C. I defined "sud" in exterior, how can i do it in python? Or do I need to pass the matrix as an argument in my work?
Sorry, if my problem looks trivial, then I am very new in Python.
Below I have the code:
N = 9f = open ('sudoku.txt', 'r') tmp = f.read (). Splitlines () for sud = [[int (x)) in the X (I)] for the TMP [I]] in the range (N)] DIF check (X, Y, W): If I [S] for [S] ] Valve in: ### Check the line X for the Wall Return 1 if I in the range 1 (N)] for [sud [i] [x] Val: ### Column for Call Returns Y 1 xx For the range in the range (ybox, ybox + 3) = = (x / 3) * 3 ybox = int (y / 3) * 3 for the range (xbox, xbox + 3): if val == sud [I] [j]: ### Check box 1 for returning return returns 0 Solution Iarf (x = 0, y = 0): if x == N: y + = 1 x = 0 if y == N: return 1 if sud [y] [x] & gt; 0: Resolve solutions (X + 1, Y) to return to range (1, N + 1): If (x, y, i) is not checked then: sud [y] [x] = i Hull (x + 1, y): print (sud) sud [or] [x] = 0 return 0
<0> (xbox, xbox + 3): for category j (ybox, ybox + 3): if val == sud [i] [j]: # # Check box 1 for returning # val; It seems that if you mix your x and y indexing on the line if ; In the rest of your code, y comes first and x comes in second place. (Xbox, xbox + 3) for category: i for category (ybox, ybox + 3): if val == revenge [j] [ii]: wal return 1 For the ### check box wipe for the , the output grows against the sample grid, which produces the output that appears to be correct.
Comments
Post a Comment