<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 DisjointSet{
 public:
       vector<int>parent,size,rank;
       DisjointSet(int n){
        parent.resize(n+1,0);
        size.resize(n+1,1);
        rank.resize(n+1,0);
        for(int i=0;i<=n;i++)parent[i]=i;
       }

       int findUPar(int node){
        if(parent[node]==node)return node;
        return parent[node]=findUPar(parent[node]);
       }

       void unionByRank(int u,int v){
          int ulp_u=findUPar(u);
          int ulp_v=findUPar(v);
          if(ulp_u==ulp_v)return;
          if(rank[ulp_u]==rank[ulp_v]){
            rank[ulp_u]++;
            parent[ulp_v]=ulp_u;
          }else if(rank[ulp_u]>rank[ulp_v]){
            parent[ulp_v]=ulp_u;
          }else{
            parent[ulp_u]=ulp_v;
          }
       }
       
};





class Solution {
 public:
    
    bool check(int row,int col,int m,int n){
        return row>=0 and col>=0 and row<m and col<n;
    }
    

    int delX[4]={-1,1,0,0};
    int delY[4]={0,0,-1,1};

    void dfs(int row,int col,vector<vector<int>>&grid,vector<vector<int>>&vis,int m,int n){
      vis[row][col]=1;
      for(int i=0;i<4;i++){
        int nrow=row+delX[i];
        int ncol=col+delY[i];
        if(check(nrow,ncol,m,n) and !vis[nrow][ncol] and grid[nrow][ncol]==0)
        dfs(nrow,ncol,grid,vis,m,n);
      }

    }
   



    int regionsBySlashes(vector<string>&mat) {
        int m=mat.size();
        int n=mat[0].size();
        //in these kind of questions where symbols are given to us,there is a pattern,we should try to map them to 0 and 1 and then approach our question
        //we will see here that after mapping,our question reduces to number of islands
        //we will map each symbol to their equivalent 3x3 matrix,why 3x3 and not 1x1 or 2x2,solely dry run and intuition
        vector<vector<int>>grid(m*3,vector<int>(n*3,0));
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(mat[i][j]=='/'){
                  grid[i*3][j*3+2]=1;
                  grid[i*3+1][j*3+1]=1;
                  grid[i*3+2][j*3]=1;
                }else if(mat[i][j]=='\\'){
                  grid[i*3][j*3]=1;
                  grid[i*3+1][j*3+1]=1;
                  grid[i*3+2][j*3+2]=1;
                }
            }
        }

       //now our matrix of 0 and 1 is created all that left is number of islands where islands represent 0
       int regions=0;
       int m1=grid.size();
       int n1=grid[0].size();
       DisjointSet ds(m1*n1);
       for(int row=0;row<m1;row++){
        for(int col=0;col<n1;col++){
            if(grid[row][col]==1)continue;
            int delX[4]={0,0,-1,1};
            int delY[4]={-1,1,0,0};
            int node=row*m1+col;
            for(int i=0;i<4;i++){
              int nrow=row+delX[i];
              int ncol=col+delY[i];
              if(check(nrow,ncol,m1,n1) and grid[nrow][ncol]==0){
                int adjNode=nrow*m1+ncol;
                if(ds.findUPar(node)!=ds.findUPar(adjNode))
                ds.unionByRank(node,adjNode);
              }
            }      
        }
       }

       for(int i=0;i<m1;i++){
        for(int j=0;j<n1;j++){
            if(grid[i][j]==1)continue;
            int node=i*m1+j;
            if(ds.findUPar(node)==node)regions++;
        }
       }
 
              
       return regions;                                                                                        
    }
};
17886