Regions Cut By Slashes
Problem Statement
You get an n × n board of 1 × 1 squares. Each square holds one of three characters: a forward slash /, a backslash \, or a blank space. The lines cut the board into pieces, and the pieces that touch each other form regions.
Return the number of regions.
In C++ a single backslash is written \\ inside a string, so the row /\ is written "/\\" in the examples below.
grid, a list of n strings. Each string has length n and holds only /, \ or a space.
One integer: the number of regions.
The two slashes join into one line from the top-right corner to the bottom-left corner. It cuts the board into the top-left half and the bottom-right half.
One slash in the top-right square. The blank squares around it still touch each other, so the board stays in one piece.
The four slashes form a diamond. The inside of the diamond is one region, and each of the four outer corners is its own region.
- • n == grid.length == grid[i].length
- • 1 ≤ n ≤ 30
- • grid[i][j] is '/', '\' or a space.
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
Scale each square to 3 × 3 pixels, draw the slashes as walls, count the flood fills
Union the triangles a slash keeps together, plus the triangles that meet across each edge; count groups
Turn each square into a 3 × 3 block of pixels. A / turns the three diagonal pixels from bottom-left to top-right into walls, and a \ does the same from top-left to bottom-right. Then count the flood fills: every open pixel that is not yet visited starts one region.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Union-Find (DSU) translate into production architectures and low-level memory mechanics.
Foundations: Object-Oriented Principles in C++23
Class invariants, pure virtual interfaces, and RAII smart pointers.
Food Delivery System (Zomato Architecture)
Real-world machine coding application of algorithms and design patterns.
Pointers vs References Deep Dive
Pointer arithmetic and memory layout under the hood.
Virtual Method Table (vtable) & Memory Layout
Polymorphism, vptr overhead, and multiple inheritance in memory.