-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
67 lines (52 loc) · 1.7 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import math
from sudoku import Sudoku
def generate(size):
puzzle = Sudoku(size).difficulty(0.5).board
for row in range(size ** 2):
for cell in range(size ** 2):
if puzzle[row][cell] is None:
puzzle[row][cell] = 0
return puzzle
def possible(puzzle, size, size_sq, y, x, num):
for i in range(size_sq):
if puzzle[y][i] == num:
return False
for i in range(size_sq):
if puzzle[i][x] == num:
return False
x0 = (x//size) * size
y0 = (y//size) * size
for i in range(size):
for j in range(size):
if puzzle[y0 + i][x0 + j] == num:
return False
return True
def solve(puzzle, size, size_sq):
for y in range(size_sq):
for x in range(size_sq):
if puzzle[y][x] == 0:
for num in range(1, size_sq + 1):
if possible(puzzle, size, size_sq, y, x, num):
puzzle[y][x] = num
solve(puzzle, size, size_sq)
puzzle[y][x] = 0
return False
print_puzzle(puzzle)
input("\nPRESS ENTER TO PRESENT ANOTHER SOLUTION")
return True
def print_puzzle(puzzle):
space_buffer = (len(puzzle) // 10) + 2
for row in puzzle:
string = ""
for cell in row:
string += str(cell).ljust(space_buffer)
print("[ " + string + "]")
def main():
size = int(input("INPUT PUZZLE SIZE : "))
puzzle = generate(size)
print("PUZZLE :")
print_puzzle(puzzle)
print("\nSOLUTIONS :")
solve(puzzle, size, size**2)
if __name__ == '__main__':
main()