<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 __init__(self):
        self._cc = None
        self._par = None
        self._nc = None
        
    def _get1Drep(self, p):
        return self._nc * 2 *p[0] + p[1]
    
    def _getPar(self, p):
        p = self._get1Drep(p)
        while self._par[p] != p:
            p = self._par[p]
        return p
    
    def _unionF(self, p1, p2):
        par1 = self._getPar(p1)
        par2 = self._getPar(p2)
        if par1 != par2:
            if par1 > par2:
                self._par[par2] = par1
            else:
                self._par[par1] = par2
            self._cc -= 1
    
    def regionsBySlashes(self, grid: List[str]) -> int:
        if not grid: return 0
                             
        # , .
        # ; ]
        # , , . .
        # , , . .
        # ; ; ] ]
        # ; ; ] ]
        # cell i,j -> 2*i,2*j & 2*i+1,2*j & 2*i,2*j+1 & 2*i+1,2*j+1
        #               \  2*i,2*j  /
        #   2*i+1,2*j                   2*i,2*j+1
        #               /2*i+1,2*j+1\  
  
        nr = len(grid)
        self._nc = len(grid[0])
                             
        self._par = [i for i in range(nr*2*self._nc*2)]
        
        self._cc = self._nc*2*nr*2

        for i in range(nr):
            for j in range(self._nc):
                if i>0:
                    self._unionF((2*i, 2*j), (2*(i-1)+1,2*j+1))
                if j>0:
                    self._unionF((2*i+1,2*j), (2*i,2*(j-1)+1))
                if grid[i][j] == '/' or grid[i][j] == ' ':
                    self._unionF((2*i, 2*j), (2*i+1,2*j))
                    self._unionF((2*i,2*j+1), (2*i+1,2*j+1))
                if grid[i][j] == '\\' or grid[i][j] == ' ':
                    self._unionF((2*i, 2*j), (2*i,2*j+1))
                    self._unionF((2*i+1,2*j), (2*i+1,2*j+1))
                    
        return self._cc
16471