-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinder.py
More file actions
92 lines (84 loc) · 2.06 KB
/
pathfinder.py
File metadata and controls
92 lines (84 loc) · 2.06 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
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
import sys
from collections import deque
file_name = sys.argv[1]
start = tuple([int(x) for x in sys.argv[2].split(",")])
end = tuple([int(x) for x in sys.argv[3].split(",")])
matr = []
f = open(file_name, "r", encoding="utf-8")
while True:
s = f.readline().strip("\n")
if s == "":
break
else:
lst = [x for x in str(s)]
matr.append(lst)
f.close()
field = []
for i in range(len(matr)):
field.append([])
for j in range(len(matr[0])):
field[i].append(0)
i, j = start
field[i][j] = 1
def step(n):
h = len(matr)
w = len(matr[0])
for i in range(h):
for j in range(w):
if field[i][j] == n - 1:
if i - 1 >= 0 and field[i-1][j] == 0 and matr[i-1][j] == " ":
field[i-1][j] = n
if j - 1 >= 0 and field[i][j-1] == 0 and matr[i][j-1] == " ":
field[i][j-1] = n
if i + 1 < h and field[i+1][j] == 0 and matr[i+1][j] == " ":
field[i+1][j] = n
if j + 1 < w and field[i][j+1] == 0 and matr[i][j+1] == " ":
field[i][j+1] = n
return None
num = 2
i, j = end
while True:
step(num)
num += 1
if field[i][j] != 0:
break
def bfs(i, j, k):
h = len(matr)
w = len(matr[0])
vertices = deque([(i,j)])
visited = [(i,j)]
while vertices:
vertex = vertices.popleft()
i, j = vertex[0], vertex[1]
neighbors = []
if i+1 < h and k > 1:
if field[i+1][j] == k-1:
neighbors.append((i+1,j))
k -= 1
if i-1 >= 0 and k > 1:
if field[i-1][j] == k-1:
neighbors.append((i-1,j))
k -= 1
if j+1 < w and k > 1:
if field[i][j+1] == k-1:
neighbors.append((i,j+1))
k -= 1
if j-1 >= 0 and k > 1:
if field[i][j-1] == k - 1:
neighbors.append((i,j-1))
k-=1
for neighbor in neighbors:
if neighbor not in visited:
visited.append(neighbor)
vertices.append(neighbor)
visited.reverse()
return visited
h = len(field)
w = len(field[0])
for i in field:
print(" ".join([str(l).rjust(2) for l in i]))
print("\n")
i, j = end
k = field[i][j]
print(bfs(i,j, k))
#python3 pathfinder.py "maze2.txt" "1,1" "5,1"