<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 {
    vector<int> visited;
    set<pair<int, int>> ans;
    int cnt = 0;
    void connectBoundaries(vector<vector<int>>& adj){
        int n = sqrt(adj.size());
        for(int i = 0; i < n - 1; i++){
            adj[i].push_back(i + 1);
            adj[i + 1].push_back(i);
            adj[(n - 1) * (n) + i].push_back((n - 1) * (n) + i + 1);
            adj[(n - 1) * (n) + i + 1].push_back((n - 1) * (n) + i);
            adj[n * i].push_back(n * (i + 1));
            adj[n * (i + 1)].push_back(n * i);
            adj[n * i + (n - 1)].push_back(n * (i + 1) + (n - 1));
            adj[n * (i + 1) + (n - 1)].push_back(n * i + (n - 1));
        }
    }

    void dfs(vector<vector<int>>& adj, int node, int parent){
        for(auto it : adj[node]){
            if(it != parent){
                if(!visited[it]) {
                    visited[it] = 1;
                    dfs(adj, it, node);
                }else{
                    cnt++;
                    ans.insert({min(it, node), max(it, node)});
                }
            }
        }
    }

public:
    int regionsBySlashes(vector<string>& grid) {
        int n = grid.size();
        vector<vector<int>> adj((n + 1) * (n + 1));
        visited.resize((n + 1) * (n + 1), 0);
        for(int i = 0; i < n; i++){
            for(int j = 0; j < n; j++){
                int node1 , node2;
                if(grid[i][j] == '\\'){
                    node1 = i * (n + 1) + j;
                    node2 = (i + 1) * (n + 1) + j + 1;
                    adj[node1].push_back(node2);
                    adj[node2].push_back(node1);
                }else if(grid[i][j] == '/'){
                    node1 = i * (n + 1) + j + 1;
                    node2 = (i + 1) * (n + 1) + j;
                    adj[node1].push_back(node2);
                    adj[node2].push_back(node1);
                }
            }
        }
        connectBoundaries(adj);
        for(int i = 0; i < (n + 1)* (n + 1); i++){
            if(!visited[i]){
                dfs(adj, i, -1);
            }
        }   
        for(auto it : ans){
            cout << it.first <<' ' << it.second << endl;
        }
        return (cnt) / 2;
    }
};
16971