Submission of Lab 1 Implementation of Search Algorithms
Submission of Lab 1 Implementation of Search Algorithms
import heapq
terrain_map = [
[1, 2, 2, 1, 4, 3, 2, 1],
[3, 2, 5, 1, 2, 4, 2, 1],
[2, 3, 5, 3, 3, 2, 4, 4],
[1, 3, 1, 1, 4, 1, 1, 2],
[3, 5, 1, 2, 4, 4, 1, 4],
[1, 3, 1, 1, 4, 5, 1, 1]
]
direction_vectors = [
(-1, 0), (-1, 1), (0, 1), (1, 1),
(1, 0), (1, -1), (0, -1), (-1, -1)
]
start = (0, 0, 0)
goal = (5, 7, 0)
frontier = []
heapq.heappush(frontier, (0, start))
cost_so_far = {start: 0}
came_from = {start: None}
Untitled 1
while frontier:
_, current = heapq.heappop(frontier)
path = []
state = next_state if (current[0], current[1]) == (goal[0],
while state is not None:
path.append(state)
state = came_from[state]
path.reverse()
return cost_so_far, path
Untitled 2
if path:
print("Path found:")
for node in path:
print(f"Node: {node}, Cost: {cost[node]}")
else:
print("No path found.")
Untitled 3