-
Notifications
You must be signed in to change notification settings - Fork 1
/
BreathFirstSearch.py
260 lines (211 loc) · 7.85 KB
/
BreathFirstSearch.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
# -*- coding: utf-8 -*-
"""Untitled9.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1eezonijs5PdWC26RkqVRC6bgfQFBpxsQ
"""
import sys
import random
import numpy as np
import heapq
SIZE = (9,9 )
import heapq as hq
import time
from multiprocessing import Queue
# size of the labyrinth (x, y)
if sys.getrecursionlimit() < SIZE[0] * SIZE[1]:
sys.setrecursionlimit(SIZE[0] * SIZE[1])
# if max recursion limit is lower than needed, adjust it
N, S, E, W = 1, 2, 4, 8
# directions translated into bitnums to store information on all cleared walls in one variable per cell
GO_DIR = {N: (0, -1), S: (0, 1), E: (1, 0), W: (-1, 0)}
# dictionary with directions translated to digging moves
REVERSE = {E: W, W: E, N: S, S: N}
# when a passage is dug from a cell, the other cell obtains the reverse passage, too
lab = list(list(0 for i in range(SIZE[0])) for j in range(SIZE[1]))
def dig(x, y):
# digs passage from a cell (x, y) in an unvisited cell
dirs = [N, E, W, S]
random.shuffle(dirs)
# shuffles directions each time for more randomness
for dir in dirs:
new_x = x + GO_DIR[dir][0]
new_y = y + GO_DIR[dir][1]
if (new_y in range(SIZE[1])) and\
(new_x in range(SIZE[0])) and\
(lab[new_y][new_x] == 0):
# checks if the new cell is not visited
lab[y][x] |= dir
lab[new_y][new_x] |= REVERSE[dir]
# if so, apply info on passages to both cells
dig(new_x, new_y)
# repeat recursively
def check():
# displays the cells' values for check-up
for i in range(SIZE[1]):
for j in range(SIZE[0]):
print(" "*(1-(lab[i][j]//10))+\
str(lab[i][j]), end='|')
print ('')
def create():
# displays the labyrinth
# prints the seed (for reference) and the lab size‚
for k in range(SIZE[0]):
print(" ", end='')
print(k, end='')
myArray =[]
line = []
#print()
#print("_" * (SIZE[0] * 2))
for i in range (0,SIZE[0] * 2+1):
line.append("_")
myArray.append(line)
for j in range(SIZE[1]):
line =[]
if j!=0:
#print("|", end='')
line.append("|")
else:
#print ("|", end='')
line.append("|")
for i in range(SIZE[0]):
if (lab[j][i] & S != 0):
#print(" ", end='')
line.append(" ")
else:
#print("_", end='')
line.append("_")
if (lab[j][i] & E != 0):
if ((lab[j][i] | lab[j][i+1]) & S != 0):
#print(" ", end='')
line.append(" ")
else:
#print("_", end='')
line.append("_")
elif (j==SIZE[1]-1) & (i==SIZE[0]-1):
#print("|", end='')
line.append("|")
else:
#print("|", end='')
line.append("|")
#print(j)
myArray.append(line)
return myArray
#myArray=np.chararray((SIZE[0],SIZE[1]))
def printArray(myArray):
#for k in range(SIZE[0]):
#print("_"+str(k)+"_", end='')
for i in range(1, 24):
print(str(i)+" ", end='')
print()
print()
for j in range (0,len(myArray)):
for i in range (0, len(myArray[1])):
print(myArray[j][i], end='')
#print(j)
print(" "+ str(j))
def Heuristic(DesiredPoint, CurrentLocation):
return np.linalg.norm(DesiredPoint-CurrentLocation)
def newMatrix(myArray):
line = []
myArray1 = []
for j in range (0,len(myArray)*3):
line = []
for i in range (0, len(myArray[0])*3):
line.append(" ")
myArray1.append(line)
for j in range (1,len(myArray)+1):
for i in range (1, len(myArray[1])+1):
#print(i)
# print(j)
if (myArray[j-1][i-1] == "_"):
myArray1[j*3-3][i*3-1] ="_"
myArray1[j*3-3][i*3-2] ="_"
myArray1[j*3-3][i*3-3] ="_"
for c in range (j*3-2,j*3):
for b in range(i*3-3,i*3):
myArray1[c][b] = " "
else :
for c in range(j*3-3,j*3):
for b in range(i*3-3,i*3):
myArray1[c][b] = " "
for j in range (1,len(myArray)+1):
for i in range (1, len(myArray[1])+1):
#print(i)
#print(j)
if (myArray[j-1][i-1] == "|"):
myArray1[j*3-3][i*3-3] ="|"
myArray1[j*3-4][i*3-3] ="|"
myArray1[j*3-5][i*3-3] ="|"
for c in range (j*3-3,j*3):
for b in range(i*3-2,i*3):
myArray1[c][b] = " "
else :
for c in range(j*3-3,j*3):
for b in range(i*3-3,i*3):
if (myArray1[c][b]== " "):
myArray1[c][b] = " "
return myArray1
#newmatrix =np.zeros((len(myArray[0])*2,len(myArray[1])*2))
seed = random.randint(0, 1000)
random.seed(seed)
dig(SIZE[0]//2, SIZE[1]//2)
# create matrix
myArray = create()
# change size
myArray = newMatrix(myArray)
for i in range (0,len(myArray)):
myArray[len(myArray)-3][i]="_"
printArray(myArray)
print("Please enter your starting x1 y1")
x1 = int(input())
y1 = int(input())
print("Please enter your desired location")
x2 = int(input())
y2 = int(input())
myArray[x1][y1] = "X"
myArray[x2][y2] = "Y"
printArray(myArray)
# Route
CurrentLocation = np.array([x1, y1])
DesiredPoint = np.array([x2,y2])
Move = np.array([[-1,0], #go up
[0,-1], #go left
[1,0], #go down
[0, 1]]) #go right
visited = []
queue = []
while not((DesiredPoint ==CurrentLocation)[0] and (DesiredPoint ==CurrentLocation)[1]):
Position0 = CurrentLocation + Move[0]
Position1 = CurrentLocation + Move[1]
Position2 = CurrentLocation + Move[2]
Position3 = CurrentLocation + Move[3]
if Position0[0] != 0 and Position0[1] != 0 and Position0[0] != len(myArray) and Position0[0] != len(myArray[1]) and myArray[Position0[0]][Position0[1]] != "|" and myArray[Position0[0]][Position0[1]] != "_" :
A = str(Position0[0])+"-"+str(Position0[1])
if A not in visited:
if A not in queue:
queue.append(A)
if Position1[0] != 0 and Position1[1] != 0 and Position1[0] != len(myArray) and Position1[0] != len(myArray[1]) and myArray[Position1[0]][Position1[1]] != "_" and myArray[Position1[0]][Position1[1]] != "|":
B = str(Position1[0])+"-"+str(Position1[1])
if B not in visited:
if B not in queue:
queue.append(B)
if Position2[0] != 0 and Position2[1] != 0 and Position2[0] != len(myArray) and Position2[0] != len(myArray[1]) and myArray[Position2[0]][Position2[1]] != "|" and myArray[Position2[0]][Position2[1]] != "_" :
C = str(Position2[0])+"-"+str(Position2[1])
if C not in visited:
if C not in queue:
queue.append(C)
if Position3[0] != 0 and Position3[1] != 0 and Position3[0] != len(myArray) and Position3[0] != len(myArray[1]) and myArray[Position3[0]][Position3[1]] != "_" and myArray[Position3[0]][Position3[1]] != "|" :
D = str(Position3[0])+"-"+str(Position3[1])
if D not in visited:
if D not in queue:
queue.append(D)
xy = queue.pop(0)
if xy not in visited:
visited.append(xy)
Location = xy.index("-")
x = int(xy[0:Location])
y = int(xy[Location+1:])
CurrentLocation = np.array([x ,y])
myArray[x][y] = "X"
printArray(myArray)