<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 {
public:
    int regionsBySlashes(vector<string>& grid) {
        // case 1 -> 2
        // ' /'
        // '/ '
        // case 2 -> 1
        // ' /'
        // '  '
        // case 3 -> 5
        // '  ///\///'
        // ' \/\/\\//'
        // '/ \ ////\'
        // '\ \////\\'
        // '/\/\/\ \\'
        // ' \\// / \'
        // '\ /  \/\\'
        // ' /\\\\//\'
        // '\\// \//\'
        // case 4 -> 4
        // '\/'
        // '/\'
        // case 5 -> 3
        // '\  /'
        // ' \/ '
        // '/ \ '
        // '   \'
        // case 6 -> 2
        // '/'
        // case 7 -> 2
        // '/ / '
        // '////'
        // ' // '
        // '/ / '
        // case 8 -> 7
        // ' \\\\//'
        // '/\/\\  '
        // '  /\ //'
        // '\ / \/\'
        // '\/ \/\/'
        // ' /  //\'
        // '\\\  / '

        // solution in O(n^2)
        // read grid from top to bottom, from left to right
        // increment int region when entering new regions (as known so far)
        // for each line, keep track of the region number of each cell bottom in vector prev
        // if existing regions connect, store the info in vector conn

        int n = grid.size();
        int minR;
        int region = 0; // up to 2*n^2
        vector<int> conn; // keeps track of which regions are connected
        conn.push_back(0);
        int prev[n]; // prev[j] is the number of the region in grid[i-1][j]
        for (int i=0; i<n; i++){
            string linei = grid[i];
            region = conn.size();
            conn.push_back(region);
            for (int j=0; j<n; j++){
                char square = linei[j];
                // region : region number of left side of the square
                // prev[j] : region number of top side of the square
                if (square=='\\') {
                    int left = region; 
                    // right <- top; if no top, new region
                    if (i!=0) {
                        int top = prev[j];
                        while (top != conn[top]) top = conn[top];
                        region = top;
                    } else {
                        region = conn.size();
                        conn.push_back(region);
                    }
                    // bottom <- left
                    prev[j] = left;
                } else {                    
                    if (i!=0 && region!=prev[j]) {
                        int left = region, lR = region;
                        int top = prev[j], tR = prev[j];
                        // connect left and top
                        while (lR != conn[lR]) lR = conn[lR];
                        while (tR != conn[tR]) tR = conn[tR];
                        minR = min(lR,tR);
                        conn[left] = minR;
                        conn[top] = minR;
                        if (minR<lR) conn[lR] = minR;
                        if (minR<tR) conn[tR] = minR;
                        region = minR;
                    }

                    if (square=='/') {
                        // right is a new region
                        region = conn.size();
                        conn.push_back(region);
                    } // else right = left so region is unchanged
                    // bottom <- right
                    prev[j] = region;
                }  
                // region : region number of right side of the square
                // prev[j] : region number of bottom side of the square
            }
        }
        int count = -1;
        for (int k=0;k<conn.size();k++) {
            if (conn[k]==k) {
                count++;
            }  
        }
        return count;
    }
};
11633