<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:
        n = len(grid)
        def cellToKey(r, c, dir):
            if grid[r][c] == " ":
                return f"{r},{c}"
            else:
                return f"{r},{c},{dir}"
        def inRange(r,c):
            return r >= 0 and c >= 0 and r <n and c < n
        explored = set()
        def getOther(dir):
            if dir == "up":
                return "down"
            return "up"
        def getDirectionFromIncidentDirection(offsetR,offsetC,val):
            if offsetR == -1:
                return "down"
            if offsetR == 1:
                return "up"
            if offsetC == -1:
                if val == "/":
                    return "down"
                elif val == "\\":
                    return "up"
            else:
                if val == "/":
                    return "up"
                elif val == "\\":
                    return "down"

        def dfs(r,c, dir):
            cell = grid[r][c]
            explored.add(cellToKey(r,c,dir))
            offsets = [(0, 1), (0, -1), (1,0), (-1,0)]
            for i in range(len(offsets)):
                offsetR, offsetC = offsets[i]
                newR, newC = r+offsetR, c+offsetC
                if not inRange(newR, newC):
                    continue

                if dir == "up":
                    if offsetR == 1 and cell != " ":
                        continue
                    if offsetC == 1 and cell == "/":
                        continue
                    if offsetC == -1 and cell == "\\":
                        continue
                elif dir == "down":
                    if offsetR == -1 and cell != " ":
                        continue
                    if offsetC == 1 and cell == "\\":
                        continue
                    if offsetC == -1 and cell == "/":
                        continue
                
                newCell = grid[newR][newC]
                newDir = getDirectionFromIncidentDirection(offsetR, offsetC, newCell)
                if not cellToKey(newR, newC, newDir) in explored:
                    dfs(newR, newC, newDir)
        ans = 0
        for r in range(n):
            for c in range(n):
                if cellToKey(r,c,"up") not in explored:

                    dfs(r,c,"up")
                    ans+=1
                if cellToKey(r,c,"down") not in explored:
        
                    dfs(r,c,"down")
                    ans+=1
        return ans
20713