← 返回 apple 的题目列表Shortest Path in 2D Map with Obstacles
类型:online_judge
Given a starting point on a 2D map, find the shortest path to a target point on the map.
The map is represented by an m x n grid where each cell can be an empty space or an obstacle. You can move to adjacent empty spaces, but you cannot pass through obstacles.
Please write a function def shortest_path_with_obstacles(grid: List[List[int]], start: Tuple[int, int], end: Tuple[int, int]) -> int: that returns the shortest path length from the starting point to the target point with the following conditions:
If the starting point is the same as the target point, return 0.
If the target point is unreachable, return -1.
You can move up, down, left, or right, but not diagonally.
Example Input:
grid = [[0, 1, 0], [0, 0, 0], [1, 1, 0]] start = (0, 0) end = (2, 2)
Example Output:
4
Example
Input
3 3
0 1 0
0 0 0
1 1 0
0 0
2 2