dsa1 min read
As Far from Land as Possible — Multi-Source BFS
Find the water cell farthest from any land. Multi-source BFS from all land cells gives each water cell its min distance to land.
Read →
1575 articles
Find the water cell farthest from any land. Multi-source BFS from all land cells gives each water cell its min distance to land.
Search for a word in a grid by moving to adjacent cells without reuse. DFS with backtracking: mark visited, recurse, unmark.
Count islands with distinct shapes. Encode each island's DFS traversal path as a string to capture shape, store in a set.
Count islands fully surrounded by water (no border touch). Flood-fill border land first, then count remaining connected land components.
Find minimum flips to connect two islands. DFS to find and color first island, then BFS outward until reaching second island.