-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolver.py
More file actions
48 lines (36 loc) · 1.3 KB
/
solver.py
File metadata and controls
48 lines (36 loc) · 1.3 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
from functools import reduce
MOVES = ((1,0), (0,1), (-1,0), (0,-1))
ADD = lambda a, b: tuple(map(sum, zip(a,b)))
class Game:
def __init__(self, dim, pairs):
self.dim = dim
self.pairs = pairs
self.ends = tuple([pair[1] for pair in pairs])
self.steps= [list(pair[:1]) for pair in pairs]
def _isOver(self):
return self._countEmpty() == 0
def _getFilled(self):
return tuple(reduce(lambda a, b: a+b, self.steps))
def _countEmpty(self):
return self.dim**2 - len(self._getFilled())
def _isOnBoard(self, point):
return 0 <= point[0] < self.dim and 0 <= point[1] < self.dim
def _isMovable(self, point, n):
occupied = self._getFilled() + self.ends[:n] + self.ends[n+1:]
return self._isOnBoard(point) and point not in occupied
def solve(self, n=0):
if self._isOver():
return True
if not n < len(self.pairs):
return False
cur = self.steps[n][-1]
if cur in self.ends:
return self.solve(n+1)
for dir in MOVES:
poi = ADD(cur, dir)
if self._isMovable(poi, n):
self.steps[n].append(poi)
if self.solve(n):
return True
self.steps[n].pop()
return False