<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 Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        """
        Connect the white space
        Backslash charcater despite being represented as '\\' is actually counted
        and interated on as if its just a simple char.
        While we consider an empty space as a node in out graph, we consider each '/' and '\\'
        as two nodes. Thus to apply union_find we need to choose an encoding scheme for each node:
        (i, j, ' ') -> (i * n + j) * 10
        (i, j, '/') -> (i * n + j + 0.1/0.4) * 10
        (i, j, '\') -> (i * n + j + 0.2/0.3) * 10
        """
        n = len(grid)
        dirs = [(1, 0), (0, 1)]
        parent = dict()
        rank = dict()
        def find(x):
            if parent[x] != x:
                parent[x] = find(parent[x])
            return parent[x]
        def union(x, y):
            a, b = find(x), find(y)
            if rank[a] < rank[b]:
                parent[a] = b
            elif rank[b] < rank[a]:
                parent[b] = a
            else:
                parent[b] = a
                rank[a] += 1
        for i in range(n):
            for j in range(n):
                if grid[i][j] == ' ':
                    parent[(i * n + j) * 10] = (i * n + j) * 10
                    rank[(i * n + j) * 10] = 1
                elif grid[i][j] == '/':
                    parent[(i * n + j) * 10 + 1] = (i * n + j) * 10 + 1
                    rank[(i * n + j) * 10 + 1] = 1
                    parent[(i * n + j) * 10 + 4] = (i * n + j) * 10 + 4
                    rank[(i * n + j) * 10 + 4] = 1
                else:
                    parent[(i * n + j) * 10 + 2] = (i * n + j) * 10 + 2
                    rank[(i * n + j) * 10 + 2] = 1
                    parent[(i * n + j) * 10 + 3] = (i * n + j) * 10 + 3
                    rank[(i * n + j) * 10 + 3] = 1
        num_comp = len(parent)
        for i in range(n):
            for j in range(n):
                if grid[i][j] == ' ':
                    u = (i * n + j) * 10
                    for di, dj in dirs:
                        if i + di == n or j + dj == n:
                            continue
                        if grid[i + di][j + dj] == ' ':
                            v = ((i + di) * n + j + dj) * 10
                            if find(u) != find(v):
                                union(u, v)
                                num_comp -= 1
                        elif grid[i + di][j + dj] == '/':
                            v1 = ((i + di) * n + j + dj) * 10 + 1
                            if find(u) != find(v1):
                                union(u, v1)
                                num_comp -= 1
                        elif grid[i + di][j + dj] == '\\':
                            v2 = ((i + di) * n + j + dj) * 10 + 2
                            v3 = ((i + di) * n + j + dj) * 10 + 3
                            if dj == 1:
                                if find(u) != find(v2):
                                    union(u, v2)
                                    num_comp -= 1
                            else:
                                if find(u) != find(v3):
                                    union(u, v3)
                                    num_comp -= 1
                elif grid[i][j] == '/':
                    u4 = (i * n + j) * 10 + 4
                    for di, dj in dirs:
                        if i + di == n or j + dj == n:
                            continue
                        if grid[i + di][j + dj] == ' ':
                            v = ((i + di) * n + j + dj) * 10
                            if find(u4) != find(v):
                                union(u4, v)
                                num_comp -= 1
                        elif grid[i + di][j + dj] == '/':
                            v1 = ((i + di) * n + j + dj) * 10 + 1
                            if find(u4) != find(v1):
                                union(u4, v1)
                                num_comp -= 1
                        elif grid[i + di][j + dj] == '\\':
                            v2 = ((i + di) * n + j + dj) * 10 + 2
                            v3 = ((i + di) * n + j + dj) * 10 + 3
                            if dj == 1:
                                if find(u4) != find(v2):
                                    union(u4, v2)
                                    num_comp -= 1
                            else:
                                if find(u4) != find(v3):
                                    union(u4, v3)
                                    num_comp -= 1
                elif grid[i][j] == '\\':
                    u2 = (i * n + j) * 10 + 2
                    u3 = (i * n + j) * 10 + 3
                    for di, dj in dirs:
                        if i + di == n or j + dj == n:
                            continue
                        if grid[i + di][j + dj] == ' ':
                            v = ((i + di) * n + j + dj) * 10
                            if dj == 1:
                                if find(u3) != find(v):
                                    union(u3, v)
                                    num_comp -= 1
                            else:
                                if find(u2) != find(v):
                                    union(u2, v)
                                    num_comp -= 1
                        elif grid[i + di][j + dj] == '/':
                            v1 = ((i + di) * n + j + dj) * 10 + 1
                            if dj == 1:
                                if find(u3) != find(v1):
                                    union(u3, v1)
                                    num_comp -= 1
                            else:
                                if find(u2) != find(v1):
                                    union(u2, v1)
                                    num_comp -= 1
                        elif grid[i + di][j + dj] == '\\':
                            v2 = ((i + di) * n + j + dj) * 10 + 2
                            v3 = ((i + di) * n + j + dj) * 10 + 3
                            if dj == 1:
                                if find(u3) != find(v2):
                                    union(u3, v2)
                                    num_comp -= 1
                            else:
                                if find(u2) != find(v3):
                                    union(u2, v3)
                                    num_comp -= 1           
        return num_comp 
21039