-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBackTracking.py
More file actions
48 lines (40 loc) · 1.36 KB
/
Copy pathtestBackTracking.py
File metadata and controls
48 lines (40 loc) · 1.36 KB
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
from classes import *
from startScreen import *
import copy
def findCellsWithLeastPossibleLegal(board):
leastlen = 9
for row in range(9):
for col in range(9):
if (len(Input.getLegalValue(board)[(row, col)]) < leastlen and
(Input.getNumber(board, row, col) == 0)):
leastlen = len(Input.getLegalValue(board)[(row, col)])
least = (row, col)
return least
#From 8.17 solveMiniSudoku
def backtracking(app, board):
result = Input(copy.deepcopy(app.initialBoard))
solution = helper(board, result)
return solution
def helper(board, result):
if isThereEmptyCell(result) == False:
return result
if findCellsWithLeastPossibleLegal(result) == None:
return None
else:
row, col = findCellsWithLeastPossibleLegal(result)
val = Input.getLegalValue(result)[(row, col)]
for num in val:
Input.putNumberIn(result, row, col, num)
solution = helper(board,result)
if solution != None:
return solution
Input.putNumberIn(result, row, col, 0)
return None
def isThereEmptyCell(board):
rows, cols = 9, 9
result = []
for row in range(rows):
for col in range(cols):
if Input.getNumber(board, row, col) == 0:
return True
return False