Problem 630
Regions Cut By Slashes
Medium · 20 pts · Matrix, Union Find, Depth-First Search
Problem
An N × N grid of unit squares is drawn where each square contains a '/', a '\' or nothing (shown as '.'). The slashes are diagonal walls that cut the big square into regions. Count the regions. Split every cell into 4 triangles (top, right, bottom, left) and union them according to the character in the cell: '.' joins all four, '/' joins top-left and bottom-right, '\' joins top-right and bottom-left. Neighbouring cells always join their touching triangles.
Input
The first line contains N. Each of the next N lines is a row of N characters, each '/', '\' or '.'.
Output
Print the number of regions.
Constraints
1 <= N <= 30
Example input
2 ./ /.
Example output
2
Explanation
In the 2 × 2 grid with rows ./ and /., the two slashes form one diagonal line across the whole square. It cuts the square into two regions, so the answer is 2.