-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCS3243_P2_Base_code.py
120 lines (96 loc) · 2.88 KB
/
CS3243_P2_Base_code.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
import sys
import copy
import time
# Running script: given code can be run with the command:
# python file.py, ./path/to/init_state.txt ./output/output.txt
class Sudoku(object):
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 check_col(self, number, col):
for i in range(9):
if self.puzzle[i][col] == number:
return False
return True
def check_row(self, number, row):
for i in range(9):
if self.puzzle[row][i] == number:
return False
return True
def check_square(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 check_validation(self, number, row, col):
return self.check_col(number, col) and self.check_row(number, row) and self.check_square(number, row, col)
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 range(1,10):
#print(number)
if self.check_validation(number, row, col):
#print('success')
self.puzzle[row][col] = number
if self.find_solution():
return True
self.puzzle[row][col] = 0
#print('backtrack')
return False
def solve(self):
# TODO: Write your code here
start = time.time()
self.find_solution()
end = time.time()
print(end - start)
self.ans = copy.deepcopy(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")