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

protected:

    int n;
    void DFS(vector<string>& grid, vector<vector<vector<int>>>& gridMap, int x, int y, int d){

        if (x < 0 || y < 0 || x >= n || y >= n || gridMap[x][y][d] != 0){
            return;
        }

        gridMap[x][y][d] = 1;

        switch (d){
            case 0:
                DFS(grid, gridMap, x, y - 1, 1);
                if (grid[x][y] == '\\'){
                    DFS(grid, gridMap, x, y, 3);
                } else if (grid[x][y] == '/'){
                    DFS(grid, gridMap, x, y, 2);               
                }
                break;  
            case 1:
                DFS(grid, gridMap, x, y + 1, 0);
                if (grid[x][y] == '\\'){
                    DFS(grid, gridMap, x, y, 2);
                } else if (grid[x][y] == '/'){
                    DFS(grid, gridMap, x, y, 3);               
                }
                break;  
            case 2:
                DFS(grid, gridMap, x - 1, y, 3);
                if (grid[x][y] == '\\'){
                    DFS(grid, gridMap, x, y, 1);
                }
                 else if (grid[x][y] == '/'){
                    DFS(grid, gridMap, x, y, 0);               
                }
                break;  
            case 3:
                DFS(grid, gridMap, x + 1, y, 2);
                if (grid[x][y] == '\\'){
                    DFS(grid, gridMap, x, y, 0);
                }
                 else if (grid[x][y] == '/'){
                    DFS(grid, gridMap, x, y, 1);               
                }
                break;  
            default:
                break;
        }

        if (grid[x][y] == ' '){
            for (int i = 1; i < 4; i++){
                DFS(grid, gridMap, x, y, (d + i) % 4); 
            }

        }

        return;

    }

public:
    int regionsBySlashes(vector<string>& grid) {

        // for every connected slash, the space is divided into 2

        //   \/
        //  \/\
        //  /\ \

        // graph traversal?
        // /  only traverse up and left || down and right
        // \\ only traverse down and left || up and right
        // '' connected to up, down, left, right

        // create a grid of 4n * 4n
        // [x][y][d]

        // if [x][y] = ' '
        // [x][y][left] add [x][y - 1][right] and [x][y][other three directions]
        // [x][y][right] add [x][y + 1][left] and [x][y][other three directions]
        // [x][y][up] add [x - 1][y][down] and [x][y][other three directions]
        // [x][y][down] add [x + 1][y][up] and [x][y][other three directions]

        // if [x][y] = '\\'
        // [x][y][left] add [x][y - 1][right] and [x][y][down]
        // [x][y][right] add [x][y + 1][left] and [x][y][up]
        // [x][y][up] add [x - 1][y][down] && [x][y][right]
        // [x][y][down] add [x + 1][y][up] && [x][y][left]

        // if [x][y] = '/'
        // [x][y][left] add [x][y - 1][right] and [x][y][up]
        // [x][y][right] add [x][y + 1][left] and [x][y][down]
        // [x][y][up] add [x - 1][y][down] && [x][y][left]
        // [x][y][down] add [x + 1][y][up] && [x][y][right]

        // calculate n

        // float n_f = 0.0;
        // for (char c : grid[0]){
        //     if (c == '\\'){
        //         n_f += 0.5;
        //     } else {
        //         n_f += 1.0;
        //     }
        // }

  

        // n = int(n_f);
        n = grid[0].length();

        // expand grid to n x n x 4 
        vector<vector<vector<int>>> gridMap = vector(n, vector(n, vector(4, 0)));
        // vector<string> gridString;

        // for (string s : grid){

        //     string convertedString = "";
        //     for (int i = 0; i < s.length(); i++){
        //         cout<<s[i]<<endl;
        //         if (s[i] == '\\'){
        //             convertedString += "]";
        //             i++;
        //         } else {
        //             convertedString += s[i];
        //         }
        //     }
        //                     cout<<"====="<<endl;


        //     gridString.push_back(convertedString);


        // }

        // iterate through grid and do BFS, while counting disjoint islands

        int numOfRegions = 0;

        for (int x = 0 ; x < n; x++){
            for (int y = 0; y < n; y++){
                for (int d = 0; d < 4; d++){
                    if (gridMap[x][y][d] == 0){
                        DFS(grid, gridMap, x, y, d);
                        numOfRegions++;
                    }
                }
            }
        }

        return numOfRegions;



    }
};
14073