<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{
 private:
 int n;
 vector<vector<pair<int,int>>> parent;
 vector<vector<int>> sz;
 int comp;
 public:
 DSU(int n){
    this->n=n;
    parent.resize(n,vector<pair<int,int>>(n));
    sz.resize(n,vector<int>(n,1));
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            parent[i][j]={i,j};
        }
    }
    comp=n*n;
 }
 pair<int,int> find(int i,int j){
    if(parent[i][j]==make_pair(i,j)) return {i,j};
    return parent[i][j] = find(parent[i][j].first,parent[i][j].second);
 }
 void Union(int i1,int j1,int i2,int j2){
    pair<int,int> x = find(i1,j1);
    pair<int,int> y = find(i2,j2);
    if(x==y) return;
    int ix=x.first,jx=x.second;
    int iy=y.first,jy=y.second;
    if(sz[ix][jx]>=sz[iy][jy]){
        parent[iy][jy] = x;
        sz[ix][jx]+=sz[iy][jy];
    }
    else{
        parent[ix][jx] = y;
        sz[iy][jy]+=sz[ix][jx];
    }
    comp--;
 }
 int getComp(){
    return comp;
 }
};

class Solution {
public:
    int regionsBySlashes(vector<string>& grid) {
        int ans=0;
        int n=grid.size();
        vector<vector<int>> graph(3*n,vector<int>(3*n));
        for(int i=0;i<n;i++){
            for(int j=0;j<grid[i].size();j++){
                if(grid[i][j]=='/'){
                    graph[3*i][3*j+2]=graph[3*i+1][3*j+1]=graph[3*i+2][3*j]=1;
                }
                else if(grid[i][j]=='\\'){
                    graph[3*i][3*j]=graph[3*i+1][3*j+1]=graph[3*i+2][3*j+2]=1;
                }
            }
        }
        // for(int i=0;i<3*n;i++){
        //     for(int j=0;j<3*n;j++){
        //         cout<<graph[i][j]<<" ";
        //     }
        //     cout<<endl;
        // }
        vector<pair<int,int>> dir = {{0,-1},{0,1},{1,0},{-1,0}};
        DSU dsu(3*n);
        for(int i=0;i<3*n;i++){
            for(int j=0;j<3*n;j++){
                if(graph[i][j]==1) ans--;
                else{
                    for(auto [x,y]:dir){
                        if(i+x>=0 && i+x<3*n && j+y>=0 && j+y<3*n && graph[i+x][j+y]==0){
                            dsu.Union(i,j,i+x,j+y);
                        }
                    }
                }   
            }
        }
        ans+=dsu.getComp();
        return ans;
    }
};
18953