<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 {
public:
    int regionsBySlashes(std::vector<std::string>& grid) {
        int N = grid.size() * grid[0].length() * 2;
        std::vector<std::vector<int>> graph = makeGraph(grid, N);
        std::vector<bool> visited(N);
        int islandCount = 0;
        for (int i = 0; i < N; i++) {
            if (visited[i]) {
                continue;
            }
            searchGraph(graph, visited, i);
            islandCount++;
        }
        return islandCount;
    }
private:
    inline std::vector<std::vector<int>> makeGraph(std::vector<std::string>& grid, const int N) {
        std::vector<std::vector<int>> graph(N);

        for (int i = 0; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].length(); j++) {
                int node0 = getNodeNumber(i, j, grid[0].length());
                int node1 = getNodeNumber(i, j, grid[0].length(), 1);
                //std::cout << "node0=" << node0 << "(i="<<getI(node0,grid[0].length()) << ",j=" << getJ(node0, grid[0].length()) << ",offset=" << getOffset(node0, grid[0].length()) << ")" << ", node2=" << node1 << "(i=" << getI(node1, grid[0].length()) << ",j=" << getJ(node1, grid[0].length()) << ",offset=" << getOffset(node1,grid[0].length()) << ")" << std::endl;
                if (grid[i][j] == '/' || grid[i][j] == ' ') {
                    if (i > 0) {
                        int offsetAbove = ((grid[i - 1][j] == '\\') ? 0 : 1);
                        int nodeAbove = getNodeNumber(i - 1, j, grid[0].length(), offsetAbove);
                        graph[node0].push_back(nodeAbove);
                    }
                    if (j > 0) {
                        int offsetLeft = 1;
                        int nodeLeft = getNodeNumber(i, j - 1, grid[0].length(), offsetLeft);
                        graph[node0].push_back(nodeLeft);
                    }
                    if (i < (grid.size() - 1)) {
                        int offsetBelow = ((grid[i + 1][j] == '\\') ? 1 : 0);
                        int nodeBelow = getNodeNumber(i + 1, j, grid[0].length(), offsetBelow);
                        graph[node1].push_back(nodeBelow);
                    }
                    if (j < (grid[0].length() - 1)) {
                        int offsetRight = 0;
                        int nodeRight = getNodeNumber(i, j + 1, grid[0].length(), offsetRight);
                        graph[node1].push_back(nodeRight);
                    }
                    if (grid[i][j] == ' ') {
                        graph[node0].push_back(node1);
                        graph[node1].push_back(node0);
                    }
                }
                else if (grid[i][j] == '\\') {
                    if (i > 0) {
                        int offsetAbove = ((grid[i - 1][j] == '\\') ? 0 : 1);
                        int nodeAbove = getNodeNumber(i - 1, j, grid[0].length(), offsetAbove);
                        graph[node1].push_back(nodeAbove);
                    }
                    if (j > 0) {
                        int offsetLeft = 1;
                        int nodeLeft = getNodeNumber(i, j - 1, grid[0].length(), offsetLeft);
                        graph[node0].push_back(nodeLeft);
                    }
                    if (i < (grid.size() - 1)) {
                        int offsetBelow = ((grid[i + 1][j] == '\\') ? 1 : 0);
                        int nodeBelow = getNodeNumber(i + 1, j, grid[0].length(), offsetBelow);
                        graph[node0].push_back(nodeBelow);
                    }
                    if (j < (grid[0].length() - 1)) {
                        int offsetRight = 0;
                        int nodeRight = getNodeNumber(i, j + 1, grid[0].length(), offsetRight);
                        graph[node1].push_back(nodeRight);
                    }
                }
            }
        }
        return graph;
    }
    inline void searchGraph(std::vector<std::vector<int>>& graph, std::vector<bool>& visited, const int startNode) {
        std::queue<int> q;
        q.push(startNode);
        while (!q.empty()) {
            int node = q.front();
            q.pop();
            for (int neighbour : graph[node]) {
                if (visited[neighbour]) {
                    continue;
                }
                visited[neighbour] = true;
                q.push(neighbour);
            }
        }
    }
    inline int getNodeNumber(const int i, const int j, const int m, int offset = 0) {
        return (i * m*2 + j*2 + offset);
    }
    inline int getI(const int nodeNumber, const int m) {
        return (nodeNumber / (m*2));
    }
    inline int getJ(const int nodeNumber, const int m) {
        return (nodeNumber % (m*2)) / 2;
    }
    inline int getOffset(const int nodeNumber, const int m) {
        return (nodeNumber % (m * 2)) % 2;
    }
};
22461