Practice/Meta/Maze Pathfinding Algorithm
AI-Enabled CodingMust
You are given a 2D grid representing a maze where each cell can either be passable or blocked by an obstacle. Your task is to find any valid path from a starting position to an ending position, navigating only through passable cells.
The grid is represented as a 2D array where:
0 represents a passable cell you can move through1 represents an obstacle you cannot pass throughYou can move in four directions: up, down, left, and right (no diagonal movement). Return a path as a list of coordinates from start to end, or an empty list if no path exists.
00)Example 1:
Input: maze = [[0, 0, 0], [0, 1, 0], [0, 0, 0]] start = (0, 0) end = (2, 2) Output: [(0, 0), (0, 1), (0, 2), (1, 2), (2, 2)] Explanation: The path navigates around the obstacle at position (1, 1) by going right along the top row, then down the right column.
Example 2:
Input: maze = [[0, 1], [1, 0]] start = (0, 0) end = (1, 1) Output: [] Explanation: No path exists because all routes are blocked by obstacles.
Example 3:
Input: maze = [[0, 0, 0], [1, 1, 1], [0, 0, 0]] start = (0, 0) end = (0, 2) Output: [(0, 0), (0, 1), (0, 2)] Explanation: The shortest path goes directly along the top row.