current position:Home>Python program cannot run Sudoku

Python program cannot run Sudoku

2022-05-15 06:21:07CSDN Q & A

How to use it? python Do a Sudoku program ? After I tried, I found that the operation has been unsuccessful , If it's stuck in the second line, you won't continue to modify it .
My basic idea is to use a 9*9 Put Sudoku data in a two-dimensional array , Use one guess Function to fill in numbers , Use one check Function to determine whether the newly filled number is repeated , Then recursively use guess Fill in the numbers .
The specific code is as follows .

'''
Use a Array to record Sudoku information , And modify the modifiable grid , Until Sudoku is completed . Use b Array records Sudoku original information , It is used to judge whether the square can be modified
'''
def guess(i,j):
m=i
n=j+1
# Using variables m,n Calculate the next square
if n>8:
m+=1
n=0
if m==9:
m=8
n=8
# When n be equal to 9 when , Sudoku should change its line . The next step is to verify whether the grid can modify the data ( When inputting numbers, only Sudoku 0 You can change the data only if you use the square of )
while(b[m][n]!=0):
if (m==8 and n==8):
m=i
n=j
elif n < 8:
n+=1
else:
m+=1
n=0
#print('the next blank is a',m,n)
# Here we find the next modifiable square ,( If there is no grid that can be modified later , Then return the square being modified to break while loop
print(m,n)
a[i][j] += 1
# Start modifying the data
if a[i][j]==10:
print('it is not a true way',a)
a[i][j] = 0
return
# If the revised number is 10, It indicates that the previous number is not set correctly , This returns the previous level of recursion ( Call again later guess)
elif check(i,j):
# If the revised number is not 10, call check Function to check whether the number is in line with 、 In the same column 、 Conflict with other numbers in the small square
if i==m&j==n:
print('bingo,that's the answer!',a)
# If there is no conflict , And it's the last grid , That means you have successfully calculated the correct answer , Output the correct answer directly
else:
print('now i try to guess ',m,n)
guess(m,n)
# If there is no conflict , And there's a grid behind it , Then keep the data of this grid for the time being , Guess the number of the next grid .
guess(i,j)
# If the subsequent lattice returns recursively , It indicates that the number setting of this grid is wrong , Call again guess, Guess the number in this case +1 Is it feasible

def check(i,j):#check It is used to check whether the number filled in causes conflict
counter=0
for x in range(9):
if a[i][x]==a[i][j]:
counter+=1
if a[x][j]==a[i][j]:
counter+=1
idiv=int(i/3)
jdiv=int(j/3)
for o in range(3):
for p in range(3):
if a[idiv3+o][jdiv3+p]==a[i][j]:
counter+=1
if counter>3:
return 0
else:
return 1

a=[
[0,6,1,0,3,0,0,2,0],
[0,5,0,0,0,8,1,0,7],
[0,0,0,0,0,7,0,3,4],
[0,0,9,0,0,6,3,7,8],
[0,0,3,2,7,9,5,0,0],
[5,7,0,3,0,0,9,0,2],
[1,9,0,7,6,0,0,0,0],
[8,0,2,4,0,0,7,6,0],
[6,4,0,0,1,0,2,5,0]]
b=a[:]
guess(0,0)

Operation results and error reporting contents : Stuck in the first row , The second row will not run as expected , Very strange

What I want to achieve : Can work out the correct answer smoothly




Take the answer :

'''
Use a Array to record Sudoku information , And modify the modifiable grid , Until Sudoku is completed . Use b Array records Sudoku original information , It is used to judge whether the square can be modified
'''
def guess(i,j):
m=i
n=j+1
# Using variables m,n Calculate the next square
if n>8:
m+=1
n=0
if m==9:
m=8
n=8
# When n be equal to 9 when , Sudoku should change its line . The next step is to verify whether the grid can modify the data ( When inputting numbers, only Sudoku 0 You can change the data only if you use the square of )
while(b[m][n]!=0):
if (m==8 and n==8):
m=i
n=j
elif n < 8:
n+=1
else:
m+=1
n=0
#print('the next blank is a',m,n)
# Here we find the next modifiable square ,( If there is no grid that can be modified later , Then return the square being modified to break while loop
print(m,n)
a[i][j] += 1
# Start modifying the data
if a[i][j]==10:
print('it is not a true way',a)
a[i][j] = 0
return
# If the revised number is 10, It indicates that the previous number is not set correctly , This returns the previous level of recursion ( Call again later guess)
elif check(i,j):
# If the revised number is not 10, call check Function to check whether the number is in line with 、 In the same column 、 Conflict with other numbers in the small square
if i==m&j==n:
print('bingo,that's the answer!',a)
# If there is no conflict , And it's the last grid , That means you have successfully calculated the correct answer , Output the correct answer directly
else:
print('now i try to guess ',m,n)
guess(m,n)
# If there is no conflict , And there's a grid behind it , Then keep the data of this grid for the time being , Guess the number of the next grid .
guess(i,j)
# If the subsequent lattice returns recursively , It indicates that the number setting of this grid is wrong , Call again guess, Guess the number in this case +1 Is it feasible

def check(i,j):
counter=0
for x in range(9):
if a[i][x]==a[i][j]:
counter+=1
if a[x][j]==a[i][j]:
counter+=1
idiv=int(i/3)
jdiv=int(j/3)
for o in range(3):
for p in range(3):
if a[idiv3+o][jdiv3+p]==a[i][j]:
counter+=1
if counter>3:
return 0
else:
return 1

a=[
[0,6,1,0,3,0,0,2,0],
[0,5,0,0,0,8,1,0,7],
[0,0,0,0,0,7,0,3,4],
[0,0,9,0,0,6,3,7,8],
[0,0,3,2,7,9,5,0,0],
[5,7,0,3,0,0,9,0,2],
[1,9,0,7,6,0,0,0,0],
[8,0,2,4,0,0,7,6,0],
[6,4,0,0,1,0,2,5,0]]
b=a[:]
guess(0,0)


copyright notice
author[CSDN Q & A],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205040925500190.html

Random recommended