|
| 1 | +import numpy as np |
| 2 | + |
| 3 | +grid = [[0, 0, 6, 0, 0, 0, 7, 5, 1], |
| 4 | + [2, 0, 0, 7, 0, 0, 0, 9, 0], |
| 5 | + [0, 5, 0, 0, 0, 6, 4, 8, 0], |
| 6 | + [9, 0, 2, 0, 7, 0, 0, 0, 4], |
| 7 | + [3, 7, 4, 0, 0, 0, 8, 0, 0], |
| 8 | + [1, 8, 5, 4, 2, 9, 0, 0, 7], |
| 9 | + [0, 0, 1, 9, 0, 0, 0, 7, 0], |
| 10 | + [0, 0, 9, 0, 5, 0, 1, 0, 0], |
| 11 | + [0, 2, 7, 6, 0, 3, 0, 4, 5]] |
| 12 | + |
| 13 | +print(np.matrix(grid), end="now solved \n") |
| 14 | + |
| 15 | + |
| 16 | +def possible(y: int, x: int, n: int): |
| 17 | + global grid |
| 18 | + for i in range(0, 9): |
| 19 | + if grid[y][i] == n: |
| 20 | + return False |
| 21 | + |
| 22 | + for i in range(0, 9): |
| 23 | + if grid[i][x] == n: |
| 24 | + return False |
| 25 | + |
| 26 | + x0 = (x // 3) * 3 |
| 27 | + y0 = (y // 3) * 3 |
| 28 | + for i in range(0, 3): |
| 29 | + for j in range(0, 3): |
| 30 | + if grid[y0 + i][x0 + j] == n: |
| 31 | + return False |
| 32 | + return True |
| 33 | + |
| 34 | + |
| 35 | +def solve() : |
| 36 | + global grid |
| 37 | + for y in range(0, 9): |
| 38 | + for x in range(0, 9): |
| 39 | + if grid[y][x] == 0: |
| 40 | + for n in range(1, 10): |
| 41 | + if possible(y, x, n): |
| 42 | + grid[y][x] = n |
| 43 | + solve() |
| 44 | + grid[y][x] = 0 |
| 45 | + |
| 46 | + return |
| 47 | + |
| 48 | + print(np.matrix(grid)) |
| 49 | + |
| 50 | + |
| 51 | +solve() |
| 52 | +print('this is ths sudoku solve') |
| 53 | + |
| 54 | + |
| 55 | + |
| 56 | + |
0 commit comments