-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbfsdfs_aux.py
55 lines (46 loc) · 1.25 KB
/
bfsdfs_aux.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
import os
from math import log
from functools import partial
N = 40 #40
def printM(M):
for j in range(N):
print([M[j*N+i] for i in range(N)])
# Quadrados são inteiros, index lineares. Devolve os quadrados a N,W,S,E
def adjacent(M,n):
v = n//N
res = ()
for p in (N,-N):
if 0 <= n+p < N*N and M[n+p] != 2:
res += n+p,
for p in (1,-1):
if v*N <= n+p < (v+1)*N and M[n+p] != 2:
res += n+p,
return res
# BFS não recursivo utilizando uma Queue
def bfs(M, start, end):
pred = [-1 for _ in range(N*N)]
adj_history = []
M[start] = 1
q = [start]
while len(q):
v = q.pop(0)
if v == end:
return pred, adj_history
for e in adjacent(M, v):
if M[e] == 0:
M[e] = 1
pred[e] = v
q.append(e)
adj_history.append(e)
return [], adj_history
# Devolve o caminho do inicio ao fim, independentemente do algoritmo utilizado
def crawlback(pred, end):
if not len(pred):
return []
path = []
i = end
while pred[i] != -1:
path.append(pred[i])
i = pred[i]
path = path[::-1]
return path[1:]