This is a sudoku solver function and I have 2 questions:
JavaScript
x
46
46
1
import numpy as np
2
3
def possible(y, x, n):
4
global sudoku
5
for i in range(9):
6
if sudoku[y][i] == n:
7
return False
8
for i in range(9):
9
if sudoku[i][x] == n:
10
return False
11
x0 = (x // 3) * 3
12
y0 = (y // 3) * 3
13
for i in range(3):
14
for j in range(3):
15
if sudoku[y0 + i][x0 + j] == n:
16
return False
17
return True
18
19
20
def solve():
21
global sudoku
22
for y in range(9):
23
for x in range(9):
24
if sudoku[y][x] == 0:
25
for n in range(1, 10):
26
if possible(y, x, n):
27
sudoku[y][x] = n
28
solve()
29
sudoku[y][x] = 0
30
return
31
print("solution:n", np.matrix(sudoku))
32
return sudoku # (This is what I have trid)
33
34
35
sudoku = [[0, 0, 0, 0, 7, 0, 0, 0, 0],
36
[0, 0, 6, 0, 0, 0, 7, 0, 0],
37
[2, 0, 0, 8, 0, 3, 0, 0, 5],
38
[0, 0, 8, 0, 0, 0, 5, 0, 0],
39
[0, 2, 0, 4, 0, 9, 0, 3, 0],
40
[9, 0, 0, 6, 0, 7, 0, 0, 2],
41
[5, 0, 9, 0, 0, 0, 3, 0, 8],
42
[0, 0, 3, 0, 0, 0, 9, 0, 0],
43
[0, 7, 0, 9, 0, 4, 6, 5, 0]]
44
solve()
45
print("solution:n", np.matrix(sudoku))
46
Why 2nd
print
print the original sudoku?JavaScript124241# This is the 1st print:
2solution:
3[[3 5 4 1 7 6 2 8 9]
4[1 8 6 2 9 5 7 4 3]
5[2 9 7 8 4 3 1 6 5]
6[4 6 8 3 1 2 5 9 7]
7[7 2 1 4 5 9 8 3 6]
8[9 3 5 6 8 7 4 1 2]
9[5 4 9 7 6 1 3 2 8]
10[6 1 3 5 2 8 9 7 4]
11[8 7 2 9 3 4 6 5 1]]
12
13# This is the 2nd print:
14solution:
15[[0 0 0 0 7 0 0 0 0]
16[0 0 6 0 0 0 7 0 0]
17[2 0 0 8 0 3 0 0 5]
18[0 0 8 0 0 0 5 0 0]
19[0 2 0 4 0 9 0 3 0]
20[9 0 0 6 0 7 0 0 2]
21[5 0 9 0 0 0 3 0 8]
22[0 0 3 0 0 0 9 0 0]
23[0 7 0 9 0 4 6 5 0]]
24
How can I get the sudoku solution (not print it), because I want to reuse the solution. (I have tried using
return sudoku
but getNone
)
Thanks:)
Advertisement
Answer
Maybe it is not elegant but I use global list all_solutions
and append duplicated solution
JavaScript
1
3
1
solution = copy.deepcopy(sudoku)
2
all_solutions.append(solution)
3
It works for your example. But I still not sure if your code is correct.
JavaScript
1
59
59
1
import numpy as np
2
import copy
3
4
def possible(y, x, n):
5
6
for i in range(9):
7
if sudoku[y][i] == n:
8
return False
9
10
for i in range(9):
11
if sudoku[i][x] == n:
12
return False
13
14
x0 = (x // 3) * 3
15
y0 = (y // 3) * 3
16
17
for i in range(3):
18
for j in range(3):
19
if sudoku[y0 + i][x0 + j] == n:
20
return False
21
22
return True
23
24
25
def solve():
26
27
for y in range(9):
28
for x in range(9):
29
if sudoku[y][x] == 0:
30
for n in range(1, 10):
31
if possible(y, x, n):
32
sudoku[y][x] = n
33
solve()
34
sudoku[y][x] = 0
35
return
36
37
print("solution:n", np.matrix(sudoku))
38
39
solution = copy.deepcopy(sudoku)
40
all_solutions.append(solution)
41
42
return
43
44
45
sudoku = [[0, 0, 0, 0, 7, 0, 0, 0, 0],
46
[0, 0, 6, 0, 0, 0, 7, 0, 0],
47
[2, 0, 0, 8, 0, 3, 0, 0, 5],
48
[0, 0, 8, 0, 0, 0, 5, 0, 0],
49
[0, 2, 0, 4, 0, 9, 0, 3, 0],
50
[9, 0, 0, 6, 0, 7, 0, 0, 2],
51
[5, 0, 9, 0, 0, 0, 3, 0, 8],
52
[0, 0, 3, 0, 0, 0, 9, 0, 0],
53
[0, 7, 0, 9, 0, 4, 6, 5, 0]]
54
55
all_solutions = []
56
solve()
57
if all_solutions:
58
print("solution:n", np.matrix(all_solutions[0]))
59
The same using return
instead of global variable.
Because solve()
returns list of solutions so it has to also return [solution]
as a list.
JavaScript
1
61
61
1
import numpy as np
2
import copy
3
4
def possible(y, x, n):
5
6
for i in range(9):
7
if sudoku[y][i] == n:
8
return False
9
10
for i in range(9):
11
if sudoku[i][x] == n:
12
return False
13
14
x0 = (x // 3) * 3
15
y0 = (y // 3) * 3
16
17
for i in range(3):
18
for j in range(3):
19
if sudoku[y0 + i][x0 + j] == n:
20
return False
21
22
return True
23
24
25
def solve():
26
27
all_solutions = []
28
29
for y in range(9):
30
for x in range(9):
31
if sudoku[y][x] == 0:
32
for n in range(1, 10):
33
if possible(y, x, n):
34
sudoku[y][x] = n
35
all_solutions += solve()
36
sudoku[y][x] = 0
37
return all_solutions
38
39
print("solution:n", np.matrix(sudoku))
40
41
solution = copy.deepcopy(sudoku)
42
43
#return all_solutions + [solution]
44
return [solution]
45
46
47
sudoku = [[0, 0, 0, 0, 7, 0, 0, 0, 0],
48
[0, 0, 6, 0, 0, 0, 7, 0, 0],
49
[2, 0, 0, 8, 0, 3, 0, 0, 5],
50
[0, 0, 8, 0, 0, 0, 5, 0, 0],
51
[0, 2, 0, 4, 0, 9, 0, 3, 0],
52
[9, 0, 0, 6, 0, 7, 0, 0, 2],
53
[5, 0, 9, 0, 0, 0, 3, 0, 8],
54
[0, 0, 3, 0, 0, 0, 9, 0, 0],
55
[0, 7, 0, 9, 0, 4, 6, 5, 0]]
56
57
all_solutions = solve()
58
59
if all_solutions:
60
print("solution:n", np.matrix(all_solutions[0]))
61