<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:
        graph = {}
        nodes = 0
        n = len(grid)
        m = 0
        for y, row in enumerate(grid):
            x = 0
            skip = False
            for c in row:
                # print(c, skip)
                # if skip:
                #     skip = False
                #     continue
                if c == " ":
                    graph[y, x] = {
                        't': nodes,
                        'b': nodes,
                        'l': nodes,
                        'r': nodes,
                    }
                    x += 1
                    nodes += 1
                elif c == "\\":
                    graph[y, x] = {
                        't': nodes,
                        'b': nodes + 1,
                        'l': nodes + 1,
                        'r': nodes,
                    }
                    x += 1
                    nodes += 2
                    skip = True
                elif c == '/':
                    graph[y, x] = {
                        't': nodes,
                        'b': nodes + 1,
                        'l': nodes,
                        'r': nodes + 1,
                    }
                    x += 1
                    nodes += 2
                m = max(m, x)
        
        # print(n, m)
        # print(graph)
        directs = ['t', 'r', 'b', 'l']
        adjacent = ['b', 'l', 't', 'r']
        visited = set()
        
        def dfs(y, x, d):
            nonlocal graph, directs, visited
            if (y, x, d) in visited or not(0 <= y < n) or not(0 <= x < m):
                return
            visited.add((y, x, d))

            node = graph[y, x][directs[d]]
            dstr = directs[(d + 1) % 4]
            if graph[y, x][dstr] == node:
                dfs(y, x, (d + 1) % 4)
            dstr = directs[(d - 1) % 4]
            if graph[y, x][dstr] == node:
                dfs(y, x, (d - 1) % 4)
            
            neb_direct = adjacent[d]
            if neb_direct == 'b':
                dfs(y - 1, x, 2)
            elif neb_direct == 'l':
                dfs(y, x + 1, 3)
            elif neb_direct == 'r':
                dfs(y, x - 1, 1)
            else:
                dfs(y + 1, x, 0)
        
        groups = 0
        for yx in graph.keys():
            for i in range(4):
                if yx + (i,) not in visited:
                    dfs(yx[0], yx[1], i)
                    groups += 1
        return groups
21909