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

#include <string>
#include <vector>
#include <ranges>
#include <cassert>
#include <array>
#include <iostream>

using node_t = size_t;
using adjacency_list = std::vector<std::vector<node_t>>;

void dfs(node_t v, std::vector<bool>& vis, const adjacency_list& adj) {
    vis[v] = true;
    for (auto u : adj[v]) {
        if (vis[u]) continue;
        dfs(u, vis, adj);
    }
}

class Solution {
public:
    int regionsBySlashes(std::vector<std::string>& grid) {
        assert(not grid.empty());

        std::vector<std::vector<size_t>> grid_indices(grid.size(), std::vector<size_t>(grid.front().size()));

        adjacency_list adj;
        size_t counter = 0;
        for (const auto& [i, str] : std::views::enumerate(grid)) {
            std::cout << str << '\n';
            for (const auto& [j, c] : std::views::enumerate(str)) {
                grid_indices[i][j] = counter++;

                adj.emplace_back();
                if (c != ' ') {
                    adj.emplace_back();
                    ++counter;
                }
                assert(adj.size() == counter);

                auto connect_horizontal = [](char c1, char c2) -> std::array<bool, 2> {
                    return {c1 != ' ', false};
                };
                auto connect_vertical = [](char c1, char c2) -> std::array<bool, 2> {
                    return {c1 == '/', c2 == '\\'};
                };

                if (i > 0) {
                    auto [incr_u, incr_v] = connect_vertical(grid[i - 1][j], c);
                    auto u = grid_indices[i - 1][j] + incr_u;
                    auto v = grid_indices[i][j] + incr_v;

                    adj[v].push_back(u);
                    adj[u].push_back(v);
                }
                if (j > 0) {
                    auto [incr_u, incr_v] = connect_horizontal(grid[i][j - 1], c);
                    auto u = grid_indices[i][j - 1] + incr_u;
                    auto v = grid_indices[i][j] + incr_v;

                    adj[u].push_back(v);
                    adj[v].push_back(u);
                }
            }
        }

        for (const auto& [i, v] : std::views::enumerate(adj)) {
            std::cout << i << ' ';
            for (auto u : v) {
                std::cout << u << ',';
            }
            std::cout << '\n';
        }

        std::vector<bool> vis(adj.size());
        size_t components = 0;
        for (auto i : std::views::iota(0uz, adj.size())) {
            if (vis[i]) continue;
            ++components;
            dfs(i, vis, adj);
        }

        return components;
    }
};
22308