-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimpleuninformed.py
51 lines (42 loc) · 1.34 KB
/
simpleuninformed.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
#Este ficheiro apresenta todas as funçoes necessarias para estratégias nao informadas, não iterativas e não limitadas!
#DFS BFS
from Board import *
from Node import *
from collections import deque
def general_search_algorithm(start,func):
visto=set()
dq=deque()
if (not start.possible()):
return -1 #impossivel de resolver
no=Node(start)
dq.append(no)
visto.add(no_tuplo(no))
while(len(dq)>0):
fnode=dq.popleft()
if fnode.NodeSolution():
x = len(visto)
print("Memory:", x)
y = len(fnode.path())-1
print("Path:", y)
return fnode.path()
child_list=fnode.expandNode()
dq=func(dq,child_list,visto)
return 0 #sem soluçao
#queueing func
def insert_bfs(deque,child_list,visto):
for i in child_list:
if no_tuplo(i) not in visto:
deque.append(i)
visto.add(no_tuplo(i))
return deque
def insert_dfs(deque,child_list,visto):
for i in child_list:
if no_tuplo(i) not in visto:
deque.appendleft(i)
visto.add(no_tuplo(i))
return deque
# dfs & bfs final
def deepth_first_search(start):
return general_search_algorithm(start,insert_dfs)
def breath_first_search(start):
return general_search_algorithm(start,insert_bfs)