<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>& arr) {
        
        int ans = 0;

        int n = arr.size();

        int m = arr[0].length();

        cout << n << " : " << m << endl;

        vector< vector< vector<bool> > > vst( n, vector< vector<bool> >( m, vector<bool>( 4, false)));


        for( int i=0; i<n; i++) {
            for( int j=0; j<m; j++) {
                for( int k=0; k<4; k++) {
                    if(vst[i][j][k]) {
                        continue;
                    }
                    ans = ans + 1;
                    queue< pair< pair<int,int>, int> > q;
                    q.push({ { i, j}, k});
                    vst[i][j][k] = true;
                    while(!q.empty()) {
                        pair< pair<int,int>, int> p = q.front();
                        q.pop();
                        int r = p.first.first;
                        int c = p.first.second;
                        int s = p.second;
                        // cout << " r : " << r << " c: " << c << " s: " << s << endl; 
                        if(arr[r][c]=='/') {
                            if((s==0) || (s==1)) {
                                if(!vst[r][c][0^1^s]) {
                                    q.push({ { r, c}, 0^1^s});
                                    vst[r][c][0^1^s] = true;
                                }
                            } else {
                                if(!vst[r][c][2^3^s]) {
                                    q.push({ { r, c}, 2^3^s});
                                    vst[r][c][2^3^s] = true;
                                }
                            }
                        } else if(arr[r][c]=='\\') {
                            if((s==0) || (s==3)) {
                                if(!vst[r][c][0^3^s]) {
                                    q.push({ { r, c}, 0^3^s});
                                    vst[r][c][0^3^s] = true;
                                }
                            } else {
                                if(!vst[r][c][2^1^s]) {
                                    q.push({ { r, c}, 2^1^s});
                                    vst[r][c][2^1^s] = true;
                                }
                            }
                        } else {
                            for( int ss=0; ss<4; ss++) {
                                if(!vst[r][c][ss]) {
                                    vst[r][c][ss] = true;
                                    q.push({{ r, c}, ss});
                                }
                            }
                        }
                        if((s==0) && (c>0) && (!vst[r][c-1][2])) {
                            vst[r][c-1][2] = true;
                            q.push({{ r, c-1}, 2});
                        } else if((s==1) && (r>0) && (!vst[r-1][c][3])) {
                            vst[r-1][c][3] = true;
                            q.push({{ r-1, c}, 3});
                        } else if((s==2) && (c+1<m) && (!vst[r][c+1][0])) {
                            vst[r][c+1][0] = true;
                            q.push({{ r, c+1}, 0});
                        } else if((s==3) && (r+1<n) && (!vst[r+1][c][1])) {
                            vst[r+1][c][1] = true;
                            q.push({{ r+1, c}, 1});
                        }
                    }

                    // cout << " ans : " << ans << endl;
                }
            }
        }

        return ans;
    }
};
20936