-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (55 loc) · 1.56 KB
/
main.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
from ucs import UCS
from ids import IDS
from gbfs import GBFS
from a_star import A_star
def manhattanDis(n, node1, node2):
x1, y1 = node1//n, node1%n
x2, y2 = node2//n, node2%n
return abs(x1-x2) + abs(y1-y2)
def heuristic(n, exitNode):
h = [0]*(n*n)
for i in range(n*n):
h[i] = manhattanDis(n, i, exitNode)
return h
def printPath(path, start, end):
if start == end:
print(end, end = " ")
else:
if path[end] == -1:
print("No path")
else:
printPath(path, start, path[end])
print(end, end = " ")
if __name__ == "__main__":
# read input
n = int(input())
graph = [0]*(n*n)
for i in range(n*n):
graph[i] = list(map(int, input().split()))
exitNode = int(input())
# calculate heuristic function
h = heuristic(n, exitNode)
print("Uniform-cost search result:")
explored, path = UCS(graph, 0, exitNode)
print(len(explored))
print(*explored)
printPath(path, 0, exitNode)
print()
print("Iterative deepening search result:")
explored, path = IDS(graph, 0, exitNode)
print(len(explored))
print(*explored)
if path == -1: print("No path")
else: print(*path)
print("Greedy Best-first search result:")
explored, path = GBFS(graph, 0, exitNode, h)
print(len(explored))
print(*explored)
printPath(path, 0, exitNode)
print()
print("A* search result:")
explored, path = A_star(graph, 0, exitNode, h)
print(len(explored))
print(*explored)
printPath(path, 0, exitNode)
print()