-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_queens.py
More file actions
127 lines (103 loc) · 3.9 KB
/
Copy path8_queens.py
File metadata and controls
127 lines (103 loc) · 3.9 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
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
def eight_queens():
b = Board()
data = { 'board' : b, 'finished' : False, 'count' : 0 }
solution = [None] * 8
backtrack(solution, -1, data)
print data['count']
def backtrack(solution, step, data):
if is_solution(solution, step, data):
process_solution(solution, step, data)
else:
step += 1
next_moves = construct_next_moves(solution, step, data)
for i, next_move in enumerate(next_moves):
solution[step] = next_move
make_move(solution, step, data)
backtrack(solution, step, data)
unmake_move(solution, step, data)
# if data['finished']:
# return
def is_solution(solution, step, data):
return step == 7 # 0-indexed
def process_solution(solution, step, data):
print data['board']
data['count'] += 1
def construct_next_moves(solution, step, data):
return data['board'].open_spaces(step - 1)
def make_move(solution, step, data):
data['board'].place_queen(solution, step)
def unmake_move(solution, step, data):
data['board'].remove_queen(solution, step)
class Board:
def __init__(self):
self.grid = [[0] * 8 for i in range(8)]
self.moves = [{ 'x' : None, 'y' : None} for i in range(8)]
def __str__(self):
print_str = ""
for i, row in enumerate(self.grid):
row_str = "|"
for j, space in enumerate(row):
row_str += "{}|".format(space if space == "Q" else " ")
print_str += "{}\n".format(row_str)
return print_str
def place_queen(self, solution, step):
y, x = solution[step]
self.moves[step]['x'] = x
self.moves[step]['y'] = y
self.block_perp(y, x)
self.block_diag(y, x)
self.grid[y][x] = "Q"
def block_perp(self, y, x):
for i in range(8):
self.grid[y][i] += 1
self.grid[i][x] += 1
def block_diag(self, y, x):
for i in range(8):
if 0 <= y - i < 8 and 0 <= x - i < 8:
self.grid[y - i][x - i] += 1
if 0 <= y + i < 8 and 0 <= x - i < 8:
self.grid[y + i][x - i] += 1
if 0 <= y - i < 8 and 0 <= x + i < 8:
self.grid[y - i][x + i] += 1
if 0 <= y + i < 8 and 0 <= x + i < 8:
self.grid[y + i][x + i] += 1
def remove_queen(self, solution, step):
y, x = solution[step]
self.grid[y][x] = 2
self.free_perp(y, x)
self.free_diag(y, x)
def free_perp(self, y, x):
for i in range(8):
self.grid[y][i] -= 1
self.grid[i][x] -= 1
def free_diag(self, y, x):
for i in range(8):
if 0 <= y - i < 8 and 0 <= x - i < 8 and self.grid[y - i][x - i] > 0:
self.grid[y - i][x - i] -= 1
if 0 <= y + i < 8 and 0 <= x - i < 8 and self.grid[y + i][x - i] > 0:
self.grid[y + i][x - i] -= 1
if 0 <= y - i < 8 and 0 <= x + i < 8 and self.grid[y - i][x + i] > 0:
self.grid[y - i][x + i] -= 1
if 0 <= y + i < 8 and 0 <= x + i < 8 and self.grid[y + i][x + i] > 0:
self.grid[y + i][x + i] -= 1
def open_spaces(self, move_step):
if move_step < 0:
start_x, start_y = 0, 0
else:
prev_x, prev_y = self.moves[move_step]['x'], self.moves[move_step]['y']
start_x = prev_x + 1 if prev_x < 7 else 0
start_y = prev_y if prev_x + 1 <= 7 else prev_y + 1
if start_y > 7:
return []
open_spaces = []
for i in range(start_y, 8):
for j in range(8):
if i == start_y and j < start_x:
continue
if self.grid[i][j] == 0:
open_spaces.append([i, j])
# print (start_x, start_y)
# print open_spaces
# print self
return open_spaces
eight_queens()