<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 DSU():
    def __init__(self,N):
        self.parent = [i for i in range(N)]
        self.size = [1 for _ in range(N)]

    def fr(self, u):
        if u == self.parent[u]:
            return u
        temp = self.fr(self.parent[u])
        self.parent[u] = temp
        return temp

    def unon(self, u, v):
        root_u, root_v = self.fr(u), self.fr(v)

        if root_u == root_v:
            return False

        if self.size[root_u] < self.size[root_v]:
            root_u, root_v = root_v, root_u

        self.size[root_u] += self.size[root_v]
        self.parent[root_v] = root_u
        return True


class Solution:
    def regionsBySlashes(self, grid: List[str]) -> int:
        n = len(grid)
        lines = [] # lines[i] = (p1, p2)

        for r in range(n):
            for c in range(n):
                if grid[r][c] != '/' and grid[r][c] != '\\':
                    continue
                p1 = (0,0)
                p2 = (0,0)
                if grid[r][c] == '/':
                    p1 = (c+1, r)
                    p2 = (c, r+1)
                elif grid[r][c] == '\\':
                    p1 = (c, r)
                    p2 = (c+1, r+1)
                lines.append((p1, p2))
                slope = (p1[1] - p2[1]) / (p1[0] - p2[0])
                intercept = p1[1] - slope * p1[0]

        k = n * 10

        for i in range(len(lines)):
            p1 = lines[i][0]
            p2 = lines[i][1]
            p1 = (p1[0] * k/n, p1[1] * k/n)
            p2 = (p2[0] * k/n, p2[1] * k/n)
            lines[i] = (p1, p2)
        
        g2 = [([0] * (k+1)) for _ in range(k+1)]

        def mark(x, y):
            for x_p in [int(floor(x))]:
                for y_p in [int(floor(y))]:
                    if 0 <= x_p <= k and 0 <= y_p <= k:
                        g2[x_p][y_p] = 1

        for line in lines:
            p1 = line[0]
            p2 = line[1]
            a = (p1[1] - p2[1]) / (p1[0] - p2[0])
            b = p1[1] - a * p1[0]
            x = min(p1[0], p2[0])
            while x <= max(p1[0], p2[0]):
                y = a*x + b
                mark(x, y)
                x += 0.5

        dsu = DSU((k+1)*(k+1))

        dx = [0, 1, 0, -1]
        dy = [1, 0, -1, 0]

        for x in range(k+1):
            for y in range(k+1):
                if g2[x][y] == 1:
                    continue
                for i in range(4):
                    nx = x + dx[i]
                    ny = y + dy[i]
                    if 0 <= nx <= k and 0 <= ny <= k and g2[nx][ny] == 0:
                        dsu.unon(x*(k+1) + y, nx*(k+1) + ny)
                    
        ans = []

        for x in range(k+1):
            for y in range(k+1):
                if g2[x][y] == 0:
                    ans.append(dsu.fr(x*(k+1) + y))

        return len(set(ans))
22561