Graph Theory37 sections · 1633 units
Open in Course

LeetCode 542 01 Matrix - Problem Statement

LeetCode 542

You have a binary matrix where each cell is either 00 or 11. For each cell, find the distance to the nearest 00.

The distance between two adjacent cells is 11. You can only move up, down, left, or right.

Return a matrix of the same size where each cell contains the distance to the nearest 00.

This is another multi-source BFS problem. Instead of spreading from one source, you spread from all 00 cells at once. Before reading on, think: why start from 00 cells instead of 11 cells? What would happen if you ran BFS from each 11 cell individually?