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

/*

*/
/*

*/
/*

get allowed directions
  empty --> all fours
   0 && / --> left and up
   1 && / --> right and down
   0 && \ --> left and down
   1 && \ --> right and up 

get part(i, j, di, dj)
   if empty
    0
    \ && (right or up) = 0
    \ && (left or down) = 1
    / && (right or down) = 0
    / && (left or up) = 1

dfs(i, j, part)
{
    check i, j are valid

    if grid[i][j] is empty
        vis[i][j][0] = vis[i][j][1] = true;
    else
        grid[i][j][part] = true;

    moves = get_allowed_directions(); // [{0, -1}, {1, 0}]
    for [di, dj] in moves:
        dfs(i + di, j + dj, get_part(i + di, j + dj, di, dj))
}

*/

class Solution {
private:
    int n, m;
    vector<vector<vector<bool> > > vis;
    pair<int, int> left = {0, -1}, right = {0, 1}, up = {-1, 0}, down = {1, 0};
    vector<string> grid;

    bool valid(int i, int j) {
        return i >= 0 && j >= 0 && i < n && j < m;
    }

    vector<pair<int, int> > get_allowed_directions(int i, int j, int part) {
        if (grid[i][j] == ' ')
            return {left, right, up, down};
        
        if (grid[i][j] == '/') {
            if (part == 0) 
                return {left, up};
            return {right, down};
        }
        if (part == 0) return {left, down};
        return {right, up};
    }

    int get_part(int i, int j, pair<int, int> mv) {
        if (grid[i][j] == ' ')
            return 0;
        
        if (grid[i][j] == '/')
        {
            if (mv == right || mv == down)
                return 0;
            return 1;
        }
        // \.
        if (mv == right || mv == up) return 0;
        return 1;
    }
    /*
        /\
        \/

    vis:
    {1,1},{0,0}
    {0,0},{0,0}


    ans=1

    */
    void dfs(int i, int j, int part) {
        if (vis[i][j][part])
            return;
        vis[i][j][part] = true;
        if (grid[i][j] == ' ')
            vis[i][j][!part] = true;

        vector<pair<int, int> > moves = get_allowed_directions(i, j, part);
        for (auto [di, dj] : moves) {
            int ni = i + di, nj = j + dj;
            if (!valid(ni, nj))
                continue;
            dfs(ni, nj, get_part(ni, nj, {di, dj}));
        }
    }
public:
    int regionsBySlashes(vector<string>& grid) {
        this->grid = grid;
        n = grid.size();
        m = grid[0].size();
        vis.resize(n, vector<vector<bool> >(m, vector<bool>(2)));
        
        int ans = 0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < m; ++j)
            {
                if (!vis[i][j][0])
                {
                    ans++;
                    dfs(i, j, 0);
                }
                if (!vis[i][j][1])
                {
                    ans++;
                    dfs(i, j, 1);
                }
            }
        }
        return ans;
    }
};
21851