-
Notifications
You must be signed in to change notification settings - Fork 0
/
solve.py
294 lines (257 loc) · 8.97 KB
/
solve.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
from enum import Enum
from dataclasses import dataclass
from queue import deque
class BoxContent(Enum):
empty = ' '
wall = '#'
block = 'X'
player = 'P'
target = 'O'
block_and_target = '@'
@property
def value(self):
if self == BoxContent.wall:
return "#"
elif self == BoxContent.block:
return "▀"
else:
return " "
class Direction(Enum):
up = 0
down = 1
left = 2
right = 3
@property
def value(self):
if self == Direction.up:
return (-1, 0)
elif self == Direction.down:
return (1, 0)
elif self == Direction.left:
return (0, -1)
elif self == Direction.right:
return (0, 1)
else:
raise Exception("unknown direction")
@property
def opposite(self):
if self == Direction.up:
return Direction.down
elif self == Direction.down:
return Direction.up
elif self == Direction.left:
return Direction.right
elif self == Direction.right:
return Direction.left
else:
raise Exception("unknown direction")
@dataclass
class Box:
content: BoxContent
x: int
y: int
traversable_t : int
id: int = -1
class Puzzle:
def __init__(self, board):
self.targets = None
self.player = None
self.boxes = []
self.board = self.create_board(board)
self.time = 0
self.set_traversable_boxes()
@property
def current_state(self):
if self.player is None:
raise Exception("player not found")
ls = [(self.player.x, self.player.y)]
for box in self.boxes:
ls.append((box.x, box.y))
return tuple(ls)
def create_board(self, board):
new_board = []
box_id = 0
targets = [ ]
for i, row in enumerate(board):
new_row = []
for j, col in enumerate(row):
tmp = Box(BoxContent(col), i, j,-1)
new_row.append(tmp)
if tmp.content == BoxContent.player:
self.player = tmp
elif tmp.content in [BoxContent.block, BoxContent.block_and_target]:
self.boxes.append(tmp)
tmp.id = box_id
box_id += 1
if tmp.content in [BoxContent.target , BoxContent.block_and_target]:
targets.append((i, j ))
new_board.append(new_row)
self.targets = targets
return new_board
def print_board(self, show_possible_moves=False):
for row in self.board:
for box in row:
buff = box.content.value
if (box.x, box.y) in self.targets:
buff = "X"
if box.content == BoxContent.block:
buff = str(box.id)
if self.is_reachable(box):
buff = "."
if box.content == BoxContent.player:
buff = "P"
if show_possible_moves:
if box in [x[0] for x in self.possible_moves()]:
buff = "!"
print(buff, end=" ")
print()
print()
def is_travelable(self, box):
return box.content in (BoxContent.empty,BoxContent.player, BoxContent.target)
def is_reachable(self, box):
return box.traversable_t == self.time
def set_traversable_boxes(self):
def dfs(box):
if self.is_reachable(box):
return
box.traversable_t = self.time
for i, j in [(0, 1), (0, -1), (1, 0), (-1, 0)]:
if self.is_travelable(self.board[box.x+i][box.y+j]):
dfs(self.board[box.x+i][box.y+j])
if self.player is None:
raise Exception("player not found")
self.time += 1
dfs(self.player)
def possible_moves(self):
result = []
for box in self.boxes:
for dir in Direction:
i, j = dir.value
oi, oj = dir.opposite.value
if self.is_reachable(self.board[box.x+i][box.y+j]) and self.is_travelable(self.board[box.x+oi][box.y+oj]):
result.append((box,dir.opposite))
return result
def swap(self, box1, box2):
if box1.content == BoxContent.wall or box2.content == BoxContent.wall:
return
board = self.board
board[box1.x][box1.y], board[box2.x][box2.y] = board[box2.x][box2.y], board[box1.x][box1.y]
(box1.x, box1.y), (box2.x, box2.y) = (box2.x, box2.y), (box1.x, box1.y)
box1.traversable_t = -1
box2.traversable_t = -1
def move_block(self, block, dir):
i, j = dir.value
oi, oj = dir.opposite.value
if self.is_reachable(self.board[block.x+oi][block.y+oj]) and self.is_travelable(self.board[block.x+i][block.y+j]):
empty = self.board[block.x+i][block.y+j]
player = self.player
self.swap(block, empty)
self.swap(player, empty)
self.set_traversable_boxes()
def go_to_state(self, state):
for box in self.boxes:
self.swap(box, self.board[state[box.id+1][0]][state[box.id+1][1]])
self.swap(self.player, self.board[state[0][0]][state[0][1]])
self.set_traversable_boxes()
def is_solved(self):
for box in self.boxes:
if (box.x, box.y) not in self.targets:
return False
return True
def print_solution(self, tail, prev):
path = []
head = tail
while True:
path.append(head)
if head in prev:
head = prev[head]
else:
break
print("Solution length: ", len(path))
for s in path[::-1]:
print('-'*50)
self.go_to_state(s)
self.print_board()
def solve(self):
cs = self.current_state
history = {cs[1:] : [cs[0]]}
prev = {}
def visited(node):
tmp = self.current_state
self.go_to_state(node)
player = node[0]
boxes = node[1:]
if boxes not in history:
self.go_to_state(tmp)
return False
for p_ in history[boxes]:
if self.is_reachable(self.board[p_[0]][p_[1]]):
self.go_to_state(tmp)
return True
self.go_to_state(tmp)
return False
def visit(node):
if node[1:] in history:
history[node[1:]].append(node[0])
else:
history[node[1:]]=[node[0]]
prev[node] = self.current_state
q = deque([cs])
while len(q) != 0 :
root = q.popleft()
self.go_to_state(root)
if self.is_solved():
print("Solved.")
print("History items: ", len(history))
print("Total states: ", sum(map(len, history.values())))
return root, prev
for box, dir in self.possible_moves():
self.move_block(box, dir)
child = self.current_state
self.go_to_state(root)
if not visited(child):
q.append(child)
visit(child)
def demo():
board = [
['#', '#', '#', '#', '#', '#', '#', '#', '#'],
['#', ' ', ' ', ' ', '#', '#', '#', '#', '#'],
['#', ' ', ' ', 'O', 'X', ' ', '#', '#', '#'],
['#', '#', ' ', 'O', '#', ' ', 'X', 'P', '#'],
['#', '#', ' ', 'O', '#', ' ', 'X', ' ', '#'],
['#', '#', '#', 'O', ' ', ' ', 'X', ' ', '#'],
['#', '#', '#', ' ', ' ', '#', '#', '#', '#'],
['#', '#', '#', '#', '#', '#', '#', '#', '#'],
]
# targets =frozenset([(2, 3), (3, 3), (4,3), (5, 3)])
puzzle_ = Puzzle(board)
puzzle_.print_board()
print("Started solving..")
root, prev = puzzle_.solve()
puzzle_.print_solution(root, prev)
def demo2():
# board = [
# [1, 1, 1, 1, 1, 1, 1 ],
# [1, 0, 0, 1, 0, 0, 1 ],
# [1, 0, 0, 2, 0, 0, 1 ],
# [1, 0, 0, 2, 0, 0, 1 ],
# [1, 0, 2, 2, 2, 0, 1 ],
# [1, 1, 0, 0, 3, 1, 1 ],
# [1, 1, 1, 1, 1, 1, 1 ],
# ]
board = [
['#', '#', '#', '#', '#', '#', '#' ],
['#', ' ', ' ', '#', ' ', ' ', '#' ],
['#', ' ', ' ', 'X', ' ', ' ', '#' ],
['#', ' ', 'O', '@', 'O', ' ', '#' ],
['#', ' ', 'X', '@', 'X', ' ', '#' ],
['#', '#', ' ', 'O', 'P', '#', '#' ],
['#', '#', '#', '#', '#', '#', '#' ],
]
# targets = frozenset([(3, 3), (4, 3), (5, 3), (3,2), (3,4)])
p = Puzzle(board)
print("Started solving..")
root, prev = p.solve()
p.print_solution(root, prev)
if __name__ == '__main__':
demo()