-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku_AC3.py
187 lines (157 loc) · 4.65 KB
/
Sudoku_AC3.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import sys
import copy
import time
from collections import deque
from copy import deepcopy
class Sudoku(object):
domain_list = {}
neighbor_list = {}
q = list()
backtrack = 0
def __init__(self, puzzle):
# you may add more attributes if you need
self.puzzle = puzzle # self.puzzle is a list of lists
self.ans = copy.deepcopy(puzzle) # self.ans is a list of lists
def find_empty_pos(self, list):
for i in range(9):
for j in range(9):
if self.puzzle[i][j] == 0:
list[0] = i
list[1] = j
return True
return False
def not_in_col(self, number, col):
for i in range(9):
if self.puzzle[i][col] == number:
return False
return True
def not_in_row(self, number, row):
for i in range(9):
if self.puzzle[row][i] == number:
return False
return True
def not_in_subgrid(self, number, row, col):
row_start = row - row % 3
col_start = col - col % 3
for i in range(3):
for j in range(3):
if self.puzzle[row_start + i][col_start + j] == number:
return False
return True
def is_valid(self, number, row, col):
return self.not_in_col(number, col) and self.not_in_row(number, row) and self.not_in_subgrid(number, row, col)
def csp(self):
for i in range(9):
for j in range(9):
if(self.puzzle[i][j] == 0): #find a variable
domain = list()
#check for possible values
for num in range(1, 10):
if(self.is_valid(num, i, j)):
domain.append(num)
#check for neighbor
neighbor = list()
#check for same row
for col in range(9):
if((self.puzzle[i][col] == 0) and (col != j)):
neighbor.append((i, col))
self.q.append(((i, j), (i ,col)))
#check for same column
for row in range(9):
if((self.puzzle[row][j] == 0) and (row != i)):
neighbor.append((row, j))
self.q.append(((i, j), (row ,j)))
#check for same subgrid
row_start = i - i%3
col_start = j - j%3
for s_i in range(3):
for s_j in range(3):
if((self.puzzle[row_start + s_i][col_start + s_j] == 0) and (row_start + s_i != i) and (col_start + s_j != j)):
neighbor.append((row_start + s_i, col_start + s_j))
self.q.append(((i, j), (row_start + s_i, col_start + s_j)))
self.domain_list[(i, j)] = domain
self.neighbor_list[(i, j)] = neighbor
def AC3(self):
while len(self.q) > 0:
x_i,x_j = self.q.pop() #x_i is key, x_j is key of neighbor
if self.revise(x_i, x_j):
if len(self.domain_list[x_i]) == 0:
return False
for x_k in self.neighbor_list[x_i]:
if x_k != x_j:
self.q.append((x_k, x_i))
return True
def revise(self, x_i, x_j): #return true if we remove a value from the domain
revised = False
for x in self.domain_list[x_i]:
flag = 0
for y in self.domain_list[x_j]:
if(y != x):
flag = 1
break
if flag == 0:
self.domain_list[x_i].remove(x)
revised = True
return revised
def find_solution(self):
list = [0,0]
if self.find_empty_pos(list) is False:
#no more empty space
return True
row = list[0]
col = list[1]
for number in self.domain_list[(row, col)]:
if self.is_valid(number, row, col):
self.puzzle[row][col] = number
if(self.find_solution()):
return True
self.puzzle[row][col] = 0
self.backtrack += 1
return False
def solve(self):
# TODO: Write your code here
start = time.time()
self.csp()
if(self.AC3() is False):
return False
else:
self.find_solution()
end = time.time()
print (end - start)
print self.backtrack
#self.time = end - start
self.ans = copy.deepcopy(self.puzzle)
# self.ans is a list of lists
return self.ans
# you may add more classes/functions if you think is useful
# However, ensure all the classes/functions are in this file ONLY
# Note that our evaluation scripts only call the solve method.
# Any other methods that you write should be used within the solve() method.
if __name__ == "__main__":
# STRICTLY do NOT modify the code in the main function here
if len(sys.argv) != 3:
print ("\nUsage: python CS3243_P2_Sudoku_XX.py input.txt output.txt\n")
raise ValueError("Wrong number of arguments!")
try:
f = open(sys.argv[1], 'r')
except IOError:
print ("\nUsage: python CS3243_P2_Sudoku_XX.py input.txt output.txt\n")
raise IOError("Input file not found!")
puzzle = [[0 for i in range(9)] for j in range(9)]
lines = f.readlines()
i, j = 0, 0
for line in lines:
for number in line:
if '0' <= number <= '9':
puzzle[i][j] = int(number)
j += 1
if j == 9:
i += 1
j = 0
sudoku = Sudoku(puzzle)
ans = sudoku.solve()
with open(sys.argv[2], 'a') as f:
for i in range(9):
for j in range(9):
f.write(str(ans[i][j]) + " ")
f.write("\n")