Regions Cut By Slashes Problem
Description
LeetCode Problem 959.
An n x n grid is composed of 1 x 1 squares where each 1 x 1 square consists of a '/', '', or blank space ' '. These characters divide the square into contiguous regions.
Given the grid grid represented as a string array, return the number of regions.
Note that backslash characters are escaped, so a '' is represented as '\'.
Example 1:
Input: grid = [" /","/ "]
Output: 2
Example 2:
Input: grid = [" /"," "]
Output: 1
Example 3:
Input: grid = ["\\/","/\\"]
Output: 4
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
Example 4:
Input: grid = ["/\\","\\/"]
Output: 5
Explanation: (Recall that because \ characters are escaped, "\\/" refers to \/, and "/\\" refers to /\.)
Example 5:
Input: grid = ["//","/ "]
Output: 3
Constraints:
- n == grid.length
- n == grid[i].length
- 1 <= n <= 30
- grid[i][j] is either '/', '', or ' '.
Solution
Approach: Union-Find on Four Triangles per Cell
The two diagonals of a cell split it into four triangles: top, right, bottom and left. Any slash runs along one of those diagonals, so a region is always a union of whole triangles. Counting regions is then counting connected groups of triangles, which union-find does well.
_______
|\ 0 /| 0 = top, 1 = right,
| \ / | 2 = bottom, 3 = left
|3 X 1|
| / \ | '/' separates {0, 3} from {1, 2}
|/__2__\| '\' separates {0, 1} from {2, 3}
Start with every triangle as its own region (4 · n2 of them) and merge, subtracting one each time two different groups are joined:
- Inside a cell: a blank joins all four triangles. A
/joins top with left and right with bottom. A\joins top with right and bottom with left. - Across cell borders, whatever the characters: the bottom triangle of a cell touches the top triangle of the cell below, and its right triangle touches the left triangle of the cell to the right.
The number of groups left at the end is the number of regions.
Step 2 is easy to forget, or to make depend on the slashes. A slash only ever cuts inside its own cell, so neighboring triangles across a border are always in the same region.
Complexity: O(n2 log n) time at worst, and in practice close to O(n2), since union-find with path halving makes each operation O(log n) amortized. O(n2) space for the parent links. Here n is the side of the grid.
class Solution:
def regionsBySlashes(self, grid: List[str]) -> int:
n = len(grid)
# Each cell is split by its two diagonals into 4 triangles: 0 = top, 1 = right, 2 = bottom, 3 = left.
parent = list(range(4 * n * n))
self.regions = 4 * n * n # every triangle starts as its own region
def find(x: int) -> int:
while parent[x] != x:
parent[x] = parent[parent[x]] # path halving
x = parent[x]
return x
def union(a: int, b: int) -> None:
root_a, root_b = find(a), find(b)
if root_a != root_b:
parent[root_a] = root_b
self.regions -= 1
for r in range(n):
for c in range(n):
cell = 4 * (r * n + c)
ch = grid[r][c]
if ch == " ":
union(cell + 0, cell + 1)
union(cell + 1, cell + 2)
union(cell + 2, cell + 3)
elif ch == "/":
union(cell + 0, cell + 3) # top joins left
union(cell + 1, cell + 2) # right joins bottom
else: # backslash
union(cell + 0, cell + 1) # top joins right
union(cell + 2, cell + 3) # bottom joins left
# Triangles that touch across a cell border are always connected.
if r + 1 < n:
union(cell + 2, 4 * ((r + 1) * n + c) + 0) # bottom with the top of the cell below
if c + 1 < n:
union(cell + 1, 4 * (r * n + c + 1) + 3) # right with the left of the next cell
return self.regions