-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku_solver.py
38 lines (34 loc) · 1.14 KB
/
sudoku_solver.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
import numpy as np
import time
'''
This is a naive sudoku solver that uses backtracking to solve the puzzle. It tries to find the solution in order from the first
empty block to the last by checking all possible options.
'''
def check(grid, num, x, y):
for i in range(9):
if grid[i][y] == num:
return False
for j in range(9):
if grid[x][j] == num:
return False
x0 = (x//3) * 3
y0 = (y//3) * 3
for i in range(3):
for j in range(3):
if grid[x0+i][y0+j] == num:
return False
return True
def solve(grid):
start = time.time()
for i in range(9 + 1):
if i==9:
return True, "Solved in %.4fs" % (time.time() - start)
for j in range(9):
if grid[i][j] == 0:
for num in range(1,10):
if check(grid, num, i, j):
grid[i][j] = num
if solve(grid)[0]:
return True, "Solved in %.4fs" % (time.time() - start)
grid[i][j] = 0
return False, "Solved in %.4fs" % (time.time() - start)