← 返回 bytedance 的题目列表Maze: BFS Traversal
类型:online_judge
Given a 2D grid m x n represented as a maze where 0 represents open space and 1 represents a wall, starting at the top-left open space in the maze. Your goal is to find a possible path using BFS to get to the bottom of the maze and output the list of coordinates of the path.
Input Description:
A 2D array maze of dimensions m x n (1 <= m, n <= 1000) where each element is 0 or 1.
Output Description:
Return the list of coordinates [ [row1, col1], [row2, col2], ... ] if a path exists, otherwise return an empty list.
Example:
Input:
maze = [[0, 1, 0, 0, 0],
[0, 1, 0, 1, 0],
[0, 0, 0, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]]
Output:
[[0, 0], [1, 0], [2, 0], [2, 1], [2, 2], [3, 2], [4, 2], [4, 3], [4, 4]]
Example
Input
maze = [[0, 1, 0, 0, 0],[0, 1, 0, 1, 0],[0, 0, 0, 1, 0],[0, 1, 1, 1, 0],[0, 0, 0, 0, 0]]