AlgoFrames

Front or back of the deque

Warm-up · Trace the algorithm · about 5 min

Follow 0-1 BFS from the top-left cell. Stepping onto a 0 cell costs 0 and onto a 1 cell costs 1. After each pop, type the cost that the next relaxed cell gets.

Example 1

Input:

{"grid":[[0,1,1],[0,0,1],[1,0,0]]}

Output:

0

The route down and right avoids every obstacle, so the answer is 0.

Constraints

  • Moves go up, right, down and left, in that order.
  • grid[0][0] is 0.

Learn the idea first: 0-1 BFS and multi-source BFS, a visual lesson.

More 0-1 BFS and multi-source BFS problems