Grid Graphs

You've traversed graphs. Now treat grids as graphs. Solve Number of Islands, rotting oranges, and multi-source BFS problems on 2D matrices.

33 lessons
154 min
Codeforces: 1500-1900LeetCode: 1600-1900

Lessons

1. Introduction to Grid Graphs

Cells as nodes, adjacency as edges

4m

2. What Grid Problems Solve

Islands, regions, distances

4m

3. When to Use Grid Techniques

Flood fill vs multi-source vs BFS

3m

4. Multi-Source BFS Pattern

The multi-source pattern

4m

5. Problem - Get All Keys

LC 864 - locks, keys, and bitmasks

3m1 problems

6. Get All Keys - Why Naive Fails

Same cell, different keys = different states

4m

7. Get All Keys - Defining the State

(row, col, keys) with bitmask

4m

8. Get All Keys - Transition

Lock needs key, key updates mask

4m

9. Get All Keys - Base Cases

Find @, count total keys first

4m

10. Get All Keys - Implementation

Queue with 3D state tracking

5m1 problems

11. Get All Keys - Time and Space

O(mn · 2^K) for K keys

3m

12. Problem - Pacific Atlantic

LC 417 - flows to both oceans

4m1 problems

13. Pacific Atlantic - Why Naive Fails

Reverse: which cells reach ocean?

4m

14. Pacific Atlantic - Defining the Approach

Two visited sets, one per ocean

4m

15. Pacific Atlantic - Core Logic

Multi-source from each border

4m

16. Pacific Atlantic - Implementation

Intersection of both reachable sets

5m

17. Problem - Surrounded Regions

LC 130 - flip surrounded Os

4m1 problems

18. Surrounded Regions - Why Naive Fails

Find unsurrounded, flip the rest

4m

19. Surrounded Regions - Defining the Approach

Mark boundary-connected first

4m

20. Surrounded Regions - Core Logic

Border DFS, then scan interior

4m

21. Surrounded Regions - Implementation

Temporary marker T, then replace

5m1 problems

22. Boundary DFS Pattern

The boundary pattern

4m

23. Problem - Shortest Bridge

LC 934 - connect two islands

4m1 problems

24. Shortest Bridge - Solution

DFS first island, BFS to second

4m

25. Shortest Bridge - Why This Works

Multi-source BFS finds shortest bridge

3m

26. Quiz: Grid Pattern Recognition

Which grid pattern?

3m1 problems

27. Quiz: Boundary Techniques

Boundary vs interior techniques

3m1 problems

28. Common Mistakes in Grid Graphs

Infinite loops, slow multi-BFS

4m

29. Problem - Minimum Moves with Rotations

LC 1210 - grid rotations

10m1 problems

30. Minimum Moves with Rotations - Solution

The solution

10m

31. Problem - Escape the Spreading Fire

LC 2258 - fire escape

10m1 problems

32. Escape the Spreading Fire - Solution

The solution

10m

33. Section Recap

Flood fill to multi-source

5m

Practice Problems

1.
Ice Cavecodeforces
2.
3.
The Labyrinthcodeforces
4.
Solve The Mazecodeforces

Ready to start learning?

Access all 33 lessons with interactive content and progress tracking.