KatInterview

Minimum Water Cells to Connect Two Land Masses

Trees & GraphsAuto-graded · 18 test cases
MediumAuto-gradedTrees & Graphsbfsgridflood-fill

Last updated

Tests ability to combine flood-fill island identification with multi-source BFS to find the shortest water path bridging two disconnected land regions in a grid.

Problem

You have an R × C grid of 0s and 1s. Each 1 is land; each 0 is water. The grid contains exactly two land masses, where each land mass is a group of 1-cells connected through up, down, left, and right neighbors.

Your goal is to build the shortest possible bridge between the two land masses by converting water cells to land. Return the minimum number of water cells that must be converted so that a 4-directional path of 1s runs continuously from one land mass to the other.

Input

  • grid: a 2-D list of integers, each either 0 (water) or 1 (land)

Output

  • An integer: the fewest water cells that need to be flipped to 1 to connect the two land masses

How to think about it

The problem reduces to a shortest-path question on an unweighted grid. The cost of a path through water equals the number of water cells it passes through, and you want to minimize that count.

A useful two-phase strategy:

  1. Identify one land mass completely. Use a flood fill (BFS or iterative DFS) starting from any land cell you find. Collect every cell that belongs to that land mass.
  2. Expand outward with multi-source BFS. Treat every cell of the first land mass as a starting point at distance 0. Spread outward through water, layer by layer. The moment you reach any cell belonging to the second land mass, the number of complete water layers you have crossed is the answer.

Constraints

  • 1 <= R, C <= 200
  • Every cell is either 0 or 1
  • The grid contains exactly two 4-directionally connected land masses
  • The two land masses are never already touching, so the answer is always at least 1
  • Avoid standard recursive DFS for the flood fill — deep grids can exceed Python's default recursion limit; use an explicit stack or BFS instead

Examples

Example 1

Input: min_bridge_flips([[0, 1, 0], [0, 0, 0], [0, 0, 1]])

Output: 2

Example 2

Input: min_bridge_flips([[1, 0, 0, 0, 1]])

Output: 3

Example 3

Input: min_bridge_flips([[1, 0], [0, 1]])

Output: 1

Hints

Hint 1

Scan the grid row by row until you find the first land cell, then use an iterative flood fill (explicit stack or BFS) to collect every cell of that island into a set — that entire set becomes your starting frontier.

Hint 2

Feed all cells of the first island into a BFS queue at distance 0, then expand level by level through water cells. Process each level completely before incrementing your step counter so that the counter tracks full water layers crossed, not individual cell visits.

Hint 3

The first time BFS reaches a cell that is land but was never part of the first island, you have touched the second island — return the current step count immediately. You do not need to finish processing that layer.