<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:
    def regionsBySlashes(self, grid: List[str]) -> int:
        # we should expand every element to a 2*2 matrix
        # * => [[**], [**]]
        # / => [[*/], [/*]]
        # ...
        visited = {}
        def pre_process(grid):
            m = len(grid) * 2
            n = len(grid[0]) * 2
            new_grid = [[' ' for _ in range(n)] for _ in range(m)]
            for idx_row in range(1, m, 2):
                for idx_col in range(1, n, 2):
                    # enumerate through the down-right index of enlarged matrix
                    elem_in_original_grid = grid[(idx_row - 1) // 2][(idx_col - 1) // 2]
                    if elem_in_original_grid == '/':
                        new_grid[idx_row][idx_col - 1] = '/'
                        new_grid[idx_row - 1][idx_col] = '/'
                    elif elem_in_original_grid == '\\':
                        new_grid[idx_row][idx_col] = '\\'
                        new_grid[idx_row - 1][idx_col - 1] = '\\'
            return new_grid
                    

        def DFS(cur_row, cur_col, new_grid, m, n):
            if new_grid[cur_row][cur_col] in '/\\':
                return
            if (cur_row, cur_col) in visited:
                return
            visited[(cur_row, cur_col)] = True
            dir_row = [-1, 1, 0, 0]
            dir_col = [0, 0, -1, 1]
            for dir in range(4):
                new_row = cur_row + dir_row[dir]
                new_col = cur_col + dir_col[dir]
                if new_row < 0 or new_row >= m or new_col < 0 or new_col >= n:
                    continue
                DFS(new_row, new_col, new_grid, m, n)
            
            

            if cur_row + 1 < m and cur_col + 1 < n:
                if new_grid[cur_row][cur_col+1] != '/' or new_grid[cur_row+1][cur_col] != '/':
                    print("going down right", cur_row, cur_col)
                    DFS(cur_row + 1, cur_col + 1, new_grid, m, n)
            if cur_row - 1 >= 0 and cur_col + 1 < n:
                if new_grid[cur_row][cur_col+1] != '\\' or new_grid[cur_row-1][cur_col] != '\\':
                    DFS(cur_row - 1, cur_col + 1, new_grid, m, n)
            if cur_row - 1 >= 0 and cur_col - 1 >= 0:
                if new_grid[cur_row-1][cur_col] != '/' or new_grid[cur_row][cur_col-1] != '/':
                    DFS(cur_row - 1, cur_col - 1, new_grid, m, n)
            if cur_row + 1 < m and cur_col -  1 >= 0:
                if new_grid[cur_row+1][cur_col] != '\\' or new_grid[cur_row][cur_col-1] != '\\':
                    DFS(cur_row + 1, cur_col - 1, new_grid, m, n)
        
        
        new_grid = pre_process(grid)
        m = len(new_grid)
        n = len(new_grid[0])

        area_count = 0
        for idx_row in range(m):
            for idx_col in range(n):
                if new_grid[idx_row][idx_col] in '/\\':
                    continue
                else:
                    if (idx_row, idx_col) not in visited:
                        print("start", idx_row, idx_col)
                        area_count += 1
                        DFS(idx_row, idx_col, new_grid, m, n)
        return area_count
18538