<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 {
    vector<int> parent, rank;
public:
    int count;
    DSU(int n)
    {
        count = n;
        parent.resize(n, -1);
        rank.resize(n, 0);
    }

    int find(int x)
    {
        if(parent[x] == -1)
        {
            parent[x] = x;
            return x;
        }
        else if(parent[x] == x)
            return x;
        else
            return parent[x] = find(parent[x]);
    }

    bool union_sets(int x, int y)
    {
        int px = find(x), py = find(y);
        if(px == py)
            return false;
        if(rank[px] > rank[py])
        {
            parent[py] = px;
        }
        else if(rank[px] < rank[py])
        {
            parent[px] = py;
        }
        else
        {
            parent[px] = py;
            rank[py]++;
        }
        count--;
        return true;
    }
};

class Solution {
public:
    int regionsBySlashes(vector<string>& grid) {
        int n = grid.size();
        DSU dsu = DSU(4 * n * n);

        //union according to slashes and cell boundaries
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < grid[i].size(); j++)
            {
                // upper triangle 0, then 1, 2, 3 clockwise
                int key0 = (i * n + j) * 4 + 0; // upper triangle
                int key1 = (i * n + j) * 4 + 1; // right triangle
                int key2 = (i * n + j) * 4 + 2; // bottom triangle
                int key3 = (i * n + j) * 4 + 3; // left triangle

                if(grid[i][j] == '/')
                {
                    dsu.union_sets(key0, key3);
                    dsu.union_sets(key1, key2);
                }
                else if(grid[i][j] == '\\')
                {
                    dsu.union_sets(key0, key1);
                    dsu.union_sets(key2, key3);
                }
                else
                {
                    dsu.union_sets(key0, key1);
                    dsu.union_sets(key2, key3);
                    dsu.union_sets(key1, key2);
                }


                if(i > 0) // union(2 of upper cell, 0 of current cell)
                {
                    int ukey2 = ((i - 1) * n + j) * 4 + 2;
                    dsu.union_sets(ukey2, key0);
                }

                if(j > 0) // union(1 of left cell and 3 of current cell)
                {
                    int lkey1 = (i * n + (j - 1)) * 4 + 1;
                    dsu.union_sets(lkey1, key3);
                }
            }
        }
        return dsu.count;
    }
};
12701