<p>An <code>n x n</code> grid is composed of <code>1 x 1</code> squares where each <code>1 x 1</code> square consists of a <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or blank space <code>&#39; &#39;</code>. These characters divide the square into contiguous regions.</p>

<p>Given the grid <code>grid</code> represented as a string array, return <em>the number of regions</em>.</p>

<p>Note that backslash characters are escaped, so a <code>&#39;\&#39;</code> is represented as <code>&#39;\\&#39;</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/1.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;/ &quot;]
<strong>Output:</strong> 2
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/2.png" style="width: 200px; height: 198px;" />
<pre>
<strong>Input:</strong> grid = [&quot; /&quot;,&quot;  &quot;]
<strong>Output:</strong> 1
</pre>

<p><strong class="example">Example 3:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2018/12/15/4.png" style="width: 200px; height: 200px;" />
<pre>
<strong>Input:</strong> grid = [&quot;/\\&quot;,&quot;\\/&quot;]
<strong>Output:</strong> 5
<strong>Explanation: </strong>Recall that because \ characters are escaped, &quot;\\/&quot; refers to \/, and &quot;/\\&quot; refers to /\.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>n == grid.length == grid[i].length</code></li>
	<li><code>1 &lt;= n &lt;= 30</code></li>
	<li><code>grid[i][j]</code> is either <code>&#39;/&#39;</code>, <code>&#39;\&#39;</code>, or <code>&#39; &#39;</code>.</li>
</ul>

class UnionFind:
    def __init__(self, n):
        self.parent = list(range(n))
        self.rank = [1 for _ in range(n)]
    def find(self, node):
        if self.parent[node] == node:
            return node
        else:
            self.parent[node] = self.find(self.parent[node])
            return self.parent[node]
    def is_root(self, node):
        return self.find(node) == node
    def union(self, node_1, node_2):
        root_1 = self.find(node_1)
        root_2 = self.find(node_2)
        if root_1 == root_2:
            return
        if self.rank[root_1] > self.rank[root_2]:
            self.parent[root_2] = root_1
        elif self.rank[root_2] > self.rank[root_1]:
            self.parent[root_1] = root_2
        else:
            self.parent[root_2] = root_1
            self.rank[root_1] += 1

class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        m = len(grid)
        n = len(grid[0])
        uf = UnionFind(4 * m *n)
        # divide every small grid into 4 trangles by diagnoals
        for idx_row in range(m):
            for idx_col in range(n):
                elem = grid[idx_row][idx_col]
                first_triangle_idx = idx_row * n * 4 + idx_col * 4 # top triangle, go clock-wise
                # intra-small-grid connections. if no slashes, then union all
                # else union by slash
                if elem == '/':
                    uf.union(first_triangle_idx, first_triangle_idx + 3)
                    uf.union(first_triangle_idx + 1, first_triangle_idx + 2)
                elif elem == '\\':
                    uf.union(first_triangle_idx, first_triangle_idx + 1)
                    uf.union(first_triangle_idx + 2, first_triangle_idx + 3)
                elif elem == ' ':
                    uf.union(first_triangle_idx, first_triangle_idx + 1)
                    uf.union(first_triangle_idx + 1, first_triangle_idx + 2)
                    uf.union(first_triangle_idx + 2, first_triangle_idx + 3)
                
                # inter-small-grid connections
                # union with the right side grid and the down side grid
                if idx_col < n - 1:
                    # union with right side grid
                    first_triangle_rhs = first_triangle_idx + 4
                    uf.union(first_triangle_idx + 1, first_triangle_rhs + 3)
                if idx_row < m - 1:
                    # union with down side grid
                    first_triangle_down = first_triangle_idx + n * 4
                    uf.union(first_triangle_idx + 2, first_triangle_down)
        
        area_count = 0
        for node in range(4*m*n):
            if uf.is_root(node):
                area_count += 1
        return area_count
16798