
Problem
Solution
Submissions
A* Search Algorithm
Certification: Advanced Level
Accuracy: 0%
Submissions: 0
Points: 15
Write a C++ program to implement the A* search algorithm to find the shortest path from a start node to a goal node in a grid. The grid may contain obstacles, and the heuristic function should be the Manhattan distance.
Example 1
- Input: Grid:
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 0 0 0
0 0 0 1 0
Start: (0, 0)
Goal: (4, 4) - Output: Path: (0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2) -> (3, 2) -> (4, 2) -> (4, 3) -> (4, 4)
- Explanation: The algorithm finds the shortest path avoiding obstacles (1's) using f(n) = g(n) + h(n), where h(n) is the Manhattan distance.
Example 2
- Input: Grid:
0 1 0 0 0
0 1 0 1 0
0 0 0 1 0
0 1 0 0 0
0 0 0 1 0
Start: (0, 0)
Goal: (2, 2) - Output: Path: (0, 0) -> (1, 0) -> (2, 0) -> (2, 1) -> (2, 2)
- Explanation: The path goes around obstacles to reach (2, 2) using the optimal route.
Constraints
- 1 ≤ Grid size ≤ 100
- Time Complexity: O(b^d), where b is the branching factor and d is the depth of the goal
- Space Complexity: O(b^d)
Editorial
My Submissions
All Solutions
Lang | Status | Date | Code |
---|---|---|---|
You do not have any submissions for this problem. |
User | Lang | Status | Date | Code |
---|---|---|---|---|
No submissions found. |
Solution Hints
- Use a priority queue to prioritize nodes with the lowest cost (f = g + h).
- Use the Manhattan distance as the heuristic function.
- Keep track of visited nodes to avoid revisiting them.
- Reconstruct the path once the goal is reached.