<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{
    int n;
    vector<int> parent;
    vector<int> size;
public:
    DSU(int _n){
        n=_n;
        parent.resize(n);
        for(int i=0;i<n;i++)
            parent[i]=i;
        size.resize(n,1);
    }
    
    int find(int u){
        if(u==parent[u])    return u;
        return parent[u]=find(parent[u]);
    }
    
    void Union(int a,int b){
        int pA=find(a);
        int pB=find(b);
        if(pA==pB)  return ;
        if(size[pA]>size[pB]){
            parent[pB]=pA;
            size[pA]+=size[pB];
        }else{
            parent[pA]=pB;
            size[pB]+=size[pA];  
        }
    }

};


int getVal(int i,int j,int m){
    return i*m+j;
} 

int numIslands(vector<vector<int>>& grid,int val) {
    int n=grid.size(),m=grid[0].size(),x;
    int maxVal=n*m+1;
    DSU dsu(maxVal);
    vector<vector<bool> > visit(n,vector<bool>(m));
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(grid[i][j]==val){
                visit[i][j]=true;
                x=getVal(i,j,m);
                if(j+1<m&&grid[i][j]==grid[i][j+1]){
                    visit[i][j+1]=true;
                    dsu.Union(x,x+1);
                }
                if(i+1<n&&grid[i][j]==grid[i][j]){
                    visit[i+1][j]=true;
                    dsu.Union(x,x+m);
                }                    
            }
        }
    }
    int cnt=0;
    set<int> s;
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            if(visit[i][j]){
                x=getVal(i,j,m);
                s.insert(dsu.find(x));
            }
        }
    }
    return s.size();
}

class Solution {
public:
    void update(vector<vector<int> > &g,int i,int j,bool cond){
        int itr=3;
        int iIdx=i*3;
        int jIdx=j*3+2;;
        if(cond){
            while(itr--){
                g[iIdx][jIdx]=1;
                iIdx++;
                jIdx--;
            }
        }else{
            jIdx=j*3;
            while(itr--){
                g[iIdx][jIdx]=1;
                iIdx++;
                jIdx++;
            }  
        }
    }
    int regionsBySlashes(vector<string>& grid) {
        int n=grid.size();
        vector<vector<int> > g(n*3,vector<int>(3*n));
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(grid[i][j]=='/')
                    update(g,i,j,true);
                else if(grid[i][j]!=' ')
                    update(g,i,j,false);
            }
        }
        return numIslands(g,0);
    }
};
18343