-
Notifications
You must be signed in to change notification settings - Fork 3
/
solver.py
93 lines (80 loc) · 2.62 KB
/
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
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
def generate_positions(n):
def backtrack(position, used):
if len(position) == n:
positions.append(position[:])
return
for i in range(n):
if not used[i] and (len(position) < 1 or abs(position[-1] - i) > 1):
position.append(i)
used[i] = True
backtrack(position, used)
position.pop()
used[i] = False
positions = []
backtrack([], [False] * n)
return positions
def solve(queen_map, name):
grid = queen_map
blocks = fill_blocks(grid)
all_pos = generate_positions(len(grid))
result = {
"name":name,
"solvable":False,
"number_solution":0,
"number_possibility":len(all_pos)
}
positions = [0] * len(grid)
for i in range(len(all_pos)):
positions = all_pos[i]
game_state = []
for j in range(len(grid)):
temp_line = [0]*len(grid)
game_state.append(temp_line)
for pos in range(len(positions)):
game_state[pos][positions[pos]] = 1
if check_win(game_state, blocks):
result['solvable'] = True
result['number_solution'] += 1
return result
def check_win(game_state, blocks):
# Check rows and columns
for i in range(len(game_state)):
copied_array = game_state[i][:]
sum_row = sum(copied_array)
if sum_row != 1:
return False
sum_col = 0
for j in range(len(game_state[i])):
sum_col += game_state[j][i]
if sum_col != 1:
return False
# Check diagonals
for i in range(1, len(game_state) - 1):
for j in range(1, len(game_state) - 1):
val_center = game_state[i][j]
if val_center == 1:
if game_state[i-1][j-1] == 1:
return False
if game_state[i-1][j+1] == 1:
return False
if game_state[i+1][j-1] == 1:
return False
if game_state[i+1][j+1] == 1:
return False
for key in blocks:
sum_block = 0
for coord in blocks[key]:
sum_block += game_state[coord[0]][coord[1]]
if sum_block > 1:
return False
return True
def fill_blocks(color_grid):
blocks = {}
for i in range(len(color_grid)):
for j in range(len(color_grid[i])):
color = str(color_grid[i][j])
if color in blocks:
blocks[color].append([i, j])
else:
blocks[color] = [[i, j]]
return blocks