Write a function that implements the A* search algorithm to find the shortest path in a grid.
HardProblem Statement
Write a function that implements the A* search algorithm to find the shortest path in a grid.
Sample Input
grid = [ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ] start = [ 0, 0 ] end = [ 2, 3 ]
Sample Output
The shortest path as an array of coordinates (if exists) or an empty array if no path is possible.
Limits
Time: 2s · Memory: 256 MB
Code Editor
00:00
// Implement the A* search algorithm for shortest path in a grid
function aStar(grid, start, end) {
// Define heuristic function
// Initialize open and closed sets
// Loop to find shortest path using A* algorithm
}
console.log(aStar([ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ], [ 0, 0 ], [ 2, 3 ]));
Input
grid = [ [ 0, 0, 1, 0 ], [ 0, 1, 0, 0 ], [ 0, 0, 0, 0 ] ] start = [ 0, 0 ] end = [ 2, 3 ]
Expected Output
The shortest path as an array of coordinates (if exists) or an empty array if no path is possible.