-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcviceni10.py
204 lines (181 loc) · 11.9 KB
/
cviceni10.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
import math
class Position:
def __init__(self, x, y):
self.x = x
self.y = y
def __add__(self, other):
return Position(self.x + other.x, self.y + other.y)
def __eq__(self, other):
if isinstance(other, Position):
return self.x == other.x and self.y == other.y
else:
return False
def __hash__(self):
return hash((self.x, self.y))
def __repr__(self):
return f'<{self.x}, {self.y}>'
class Maze:
def __init__(self, tiles, start, end):
self.tiles = []
for line in tiles:
l = []
for tile in line:
l.append(0 if tile == ' ' else -1)
self.tiles.append(l)
self.start = start
self.end = end
def getAt(self, pos):
return self.tiles[pos.x][pos.y]
def setAt(self, pos, value):
self.tiles[pos.x][pos.y] = value
def reset(self):
for x, line in enumerate(self.tiles):
for y, tile in enumerate(line):
pos = Position(x, y)
if self.getAt(pos) > 0:
self.setAt(pos, 0)
def printTile(self, tile, onPath=False):
c = ''
if onPath:
c = '#'
elif tile == -1:
c = '█'
elif tile == 0:
c = ' '
else:
c = '.'
print(c, end='')
def neighbours(self, pos):
return [pos + Position(dx, dy) for dx, dy in [(1, 0), (0, 1), (-1, 0), (0, -1)]]
def print(self, path=None):
if path == None:
path = set()
for x, line in enumerate(self.tiles):
for y, tile in enumerate(line):
self.printTile(tile, Position(x, y) in path)
print()
def available(self, pos):
result = []
for candidate in self.neighbours(pos):
if self.getAt(candidate) == 0:
result.append(candidate)
return result
def best(self, pos):
min = math.inf
minPos = None
found = False
for candidate in self.neighbours(pos):
val = self.getAt(candidate)
if val > 0 and val < min:
min = val
minPos = candidate
found = True
return (min if found else 0, minPos)
def dfsearch(self):
stack = [self.start]
while len(stack) > 0:
pos = stack.pop()
b, _ = self.best(pos)
self.setAt(pos, b + 1)
# self.print(set(self.path(pos)))
if pos == self.end:
return
stack += self.available(pos)
def bfsearch(self):
queue = [self.start]
while len(queue) > 0:
pos = queue.pop(0)
b, _ = self.best(pos)
self.setAt(pos, b + 1)
# self.print(set(self.path(pos)))
if pos == self.end:
return
queue += self.available(pos)
def path(self, pos=None):
if pos == None:
pos = self.end
result = []
while pos != None and pos != self.start:
result.append(pos)
_, pos = self.best(pos)
if pos == self.start:
result.append(self.start)
return result
maze = Maze(
[list(x) for x in [
'███████████',
'█ █ ██ █',
'█ █ ██ █ █',
'█ █ ██ █',
'█ ████ █',
'█ █ ██ █ ██',
'█ █ █ █',
'█ ███████ █',
'█ █ █ █ █',
'█ █ █ █',
'███████████',
]], Position(1, 1), Position(9, 9))
bigMaze = Maze(
[list(x) for x in [
'█████████████████████████████████████████████████████████████',
'█ █ █ █ █ █ █ █ █ █',
'█ ███ █ █ █ █ █████ ███ ███ █ ███ █ ███ ███ ███ █ ███████ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █████████████ ███████ █████ ███ █████ █ █ ███ ███ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ █ ███ ███ ███ █████████████ █ █████████ ███ ███ ███████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ ███ ███ █ █ █ ███ █████ █████ ███ █ █ █ █████ ███ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █████ █ ███ █████ █ █ █ ███ ███████ █ █████████ █ ███ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █ █ ███ █ ███████ █ ███ █ █ █ ███ ███████ █ █ ███ ███ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █ █ ███ █ █████████ █████ █████ ███ ███ █████ ███ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █████ █ █ █ ███ ███ █ █ █ █ █████ ███ █████████████ ███████',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ █ ███ ███ ███████ █████ ███ ███ ███ █ █ █ █ █████ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █',
'█ ███ ███████████ █ █ ███ █████ ███ ███████ ███ █ █ █ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ ███ █ ███████ ███ █ █████████ █ █████ ███████ █ ███████ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ █ █████ █ █ █████ ███ █ ███████ █████ █ █ █████ ███████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █████ █ █ ███ ███ █ █ █████ █ ███ █ █ █████ █ ███████ ███ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ ███ ███ █ ███ █ ███ ███ ███ █████ ███ █████████ █ █ █ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█████ ███ █ █ █ █ ███ ███ ███████ █████ ███ ███ █ █ █ █ █████',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ ███ █ █ █ █ █ ███ █ █ █ ███ █ █████ ███ █ ███ ███████ ███ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█████ ███ █ █ ███████ █ ███ █ ███ █████ ███ █ █ █ ███ █████ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █████ █ █ ███ █████ ███████████████████ ███████ █ ███ █ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █',
'███ █ █████ ███████ █ █ ███ █████████ █ █ █████████████ █████',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ ███ █ █████████ ███████ █ ███ ███ █ █████ ███ █ █ █ ███ ███',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ ███ ███ █ █ █ ███ ███████ █████ █ █████ ███ █ █ █ █ ███ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █████ █████ █ ███ █ █ ███ ███ █ █ ███ ███████ █████████ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ ███ █████ █ █ ███ ███ █ █ ███ ███████ █████ █ █ ███ █ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █ ███ █████ █████████ █ ███████ █████ █ █ █ ███ █ █ █ ███',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'███ █████████████████ ███ █ ███ █████ █ ███ █████ █ █ ███ ███',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █ █████ ███████ █ █ █ ███████ █████ █████ █████ ███ █ █ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ ███ ███ █████ ███ █ █████ ███████ █████ █ ███████ █ ███████',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█████████ █ ███ █ █ █ ███ █ █ █ ███ █████ █ █████ █ █ █ █ █ █',
'█ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █ █',
'█ █████ █████ █████ █████ █ █ █████ █ █ █ █ ███ █ █ █ █████ █',
'█ █ █ █ █ █ █ █ █ █',
'█████████████████████████████████████████████████████████████',
]], Position(1, 1), Position(59, 59))