<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>

// Tag: Union find, graph, 编程语言

// Create Date: 2024/8/10
// Review Date: [2024/8/13, 2024/9/14]

#include <bits/stdc++.h>  
using namespace std;


// 感觉已经忘记了merge返回true/false对于graph的imply含义...
// 法2甚至更有意思
class UnionFind
{
public:
    vector<int> rank;
    vector<int> root;

    UnionFind(const int sz): rank(sz, 1)
    {
        for (int i = 0; i < sz; ++i)
        {
            root.push_back(i);
        }
    }

    int find(const int node)
    {
        if (node == root[node])
        {
            return node;
        }

        root[node] = find(root[node]);
        return root[node];
    }

    bool merge(const int node1, const int node2)
    {
        const int root1 = find(node1);
        const int root2 = find(node2);

        if (root1 == root2)
        {
            return false;
        }

        if (rank[root1] < rank[root2])
        {
            root[root1] = root2;
        }
        else if (rank[root2] < rank[root1])
        {
            root[root2] = root1;
        }
        else
        {
            root[root1] = root2;
            ++rank[root2];
        }
        
        return true;
    }
};

class Solution {
public:
    int xy_to_node(const int x, const int y, const int n)
    {
        return y + x * (n + 1);
    }

    tuple<int, int> node_to_xy(const int node, const int n)
    {
        return {node / (n + 1), node % (n + 1)};
    }

    int regionsBySlashes(vector<string>& grid) {
        const int n = grid.size();
        const int total_points = (n + 1) * (n + 1);

        UnionFind uf(total_points);

        int ans = 1;
        // ! 不需要explicit build graph，可以在这一步就直接用union find去merge
        // Build graph
        // vector<vector<int>> graph(total_points, vector<int>());
        for (int i = 0; i < n; ++i)
        {
            int j = 0; 
            while (j < grid[i].size())
            {
                if (grid[i][j] == ' ') 
                {
                    ++j;
                    continue;
                }

                int node1 = 0;
                int node2 = 0;
                if (grid[i][j] == '/')
                {
                    node1 = xy_to_node(i + 1, j, n);
                    node2 = xy_to_node(i, j + 1, n);
                    ++j;
                }
                else
                {
                    node1 = xy_to_node(i, j, n);
                    node2 = xy_to_node(i + 1, j + 1, n);
                    // !!! j += 2;是错的, \\被当成一个字符，甚至'\\'都是单引号，"\\".size() == 1
                    ++j;
                }

                // ! 找到了一个cycle, 每形成一个cycle就会多一个region
                if (!uf.merge(node1, node2))
                {
                    ++ans;
                }
            }
        }

        // root to cluster
        unordered_map<int, vector<int>> root2cluster;
        for (int node = 0; node < total_points; ++node)
        {
            const int root = uf.find(node);
            root2cluster[root].push_back(node);
        }

        // Analyse
        for (auto it : root2cluster)
        {
            // if (it.second.size() == 1) continue;

            int connected_edge_point_cnt = 0;
            for (int node : it.second)
            {
                const auto [x, y] = node_to_xy(node, n);
                if (x == 0 || x == n || y == 0 || y == n)
                {
                    ++connected_edge_point_cnt;
                }
            }

            if (connected_edge_point_cnt != 0)
            {
                ans += (connected_edge_point_cnt - 1);
            }
        }

        return ans;
    }
};
16056