<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, nodes):
        self.parents = {node: node for node in nodes}
        self.component_sizes = {node: 1 for node in nodes}
        self.num_components = len(nodes)

    def find_root(self, node):
        while node != self.parents[node]:
            self.parents[node] = self.parents[self.parents[node]]
            node = self.parents[node]
        return node

    def unify(self, node, other_node):
        root1, root2 = self.find_root(node), self.find_root(other_node)
        if root1 != root2:
            if self.component_sizes[root1] < self.component_sizes[root2]:
                root1, root2 = root2, root1
            self.parents[root2] = root1
            self.component_sizes[root1] += self.component_sizes[root2]
            self.num_components -= 1

class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        nr, nc = len(grid), len(grid[0])
        non_edges = defaultdict(set)
        removed = set()

        for r in range(nr):
            row = deque(list(grid[r]))
            c = 0
            while row:
                cur = row.popleft()
                if cur == " ": 
                    c += 1
                    continue
                if cur == "/":
                    er, ec = (r+1) * 3 - 1, c*3
                    removed.add((er, ec))
                    removed.add((er-1, ec+1))
                    removed.add((er-2, ec+2))
                else:
                    er, ec = r*3, (c*3)
                    removed.add((er, ec))
                    removed.add((er+1, ec+1))
                    removed.add((er+2, ec+2))
                c += 1

        all_nodes = set()
        for r in range(nr*3):
            for c in range(nc*3):
                if (r, c) not in removed:
                    all_nodes.add((r, c))
        
        uf = UnionFind(all_nodes)
        edges = defaultdict(set)
        directions = [(1, 0), (0, 1), (-1, 0), (0, -1)]
        for r, c in all_nodes:
            for rd, cd in directions:
                r1,c1 = r+rd, c+cd
                if (r1, c1) not in all_nodes: continue
                edges[(r,c)].add((r1,c1))
                uf.unify((r, c), (r1, c1))
        
        return uf.num_components
            

        
21474