<p>You are given an <code>m x n</code> <code>grid</code>. Each cell of <code>grid</code> represents a street. The street of <code>grid[i][j]</code> can be:</p>

<ul>
	<li><code>1</code> which means a street connecting the left cell and the right cell.</li>
	<li><code>2</code> which means a street connecting the upper cell and the lower cell.</li>
	<li><code>3</code> which means a street connecting the left cell and the lower cell.</li>
	<li><code>4</code> which means a street connecting the right cell and the lower cell.</li>
	<li><code>5</code> which means a street connecting the left cell and the upper cell.</li>
	<li><code>6</code> which means a street connecting the right cell and the upper cell.</li>
</ul>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/main.png" style="width: 450px; height: 708px;" />
<p>You will initially start at the street of the upper-left cell <code>(0, 0)</code>. A valid path in the grid is a path that starts from the upper left cell <code>(0, 0)</code> and ends at the bottom-right cell <code>(m - 1, n - 1)</code>. <strong>The path should only follow the streets</strong>.</p>

<p><strong>Notice</strong> that you are <strong>not allowed</strong> to change any street.</p>

<p>Return <code>true</code><em> if there is a valid path in the grid or </em><code>false</code><em> otherwise</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e1.png" style="width: 455px; height: 311px;" />
<pre>
<strong>Input:</strong> grid = [[2,4,3],[6,5,2]]
<strong>Output:</strong> true
<strong>Explanation:</strong> As shown you can start at cell (0, 0) and visit all the cells of the grid to reach (m - 1, n - 1).
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/03/05/e2.png" style="width: 455px; height: 293px;" />
<pre>
<strong>Input:</strong> grid = [[1,2,1],[1,2,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> As shown you the street at cell (0, 0) is not connected with any street of any other cell and you will get stuck at cell (0, 0)
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> grid = [[1,1,2]]
<strong>Output:</strong> false
<strong>Explanation:</strong> You will get stuck at cell (0, 1) and you cannot reach cell (0, 2).
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == grid.length</code></li>
	<li><code>n == grid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 300</code></li>
	<li><code>1 &lt;= grid[i][j] &lt;= 6</code></li>
</ul>

class Solution:
    def hasValidPath(self, grid: List[List[int]]) -> bool:
        visited = set()
        ROWS, COLS = len(grid), len(grid[0])
        end = (ROWS-1, COLS-1)

        if end == (0, 0): return True

        prev = ""

        start = grid[0][0]

        if start == 1 or start == 3: prev = "right"
        if start == 2 or start == 6: prev = "down"
        if start == 5: return False

        def run(prev):
            nonlocal visited
            nonlocal grid

            curr = (0, 0)
            while curr not in visited:
                print(curr)
                if (ROWS-1, COLS-1) in visited:
                    return True

                visited.add(curr)

                r, c = curr

                if (r == ROWS and c == COLS - 1) or (r == ROWS - 1 and c == COLS):
                    return True

                if r < 0 or r >= ROWS or c < 0 or c >= COLS: 
                    return False
            
                d = ""
                cell = grid[r][c]

                if cell == 1 and prev == "right":
                    d = "right"
                elif cell == 1 and prev == "left":
                    d = "left"

                elif cell == 2 and prev == "down":
                    d = "down"
                elif cell == 2 and prev == "up":
                    d = "up"

                elif cell == 3 and prev == "right":
                    d = "down"
                elif cell == 3 and prev == "up":
                    d = "left"

                elif cell == 4 and prev == "left":
                    d = "down"
                elif cell == 4 and prev == "up":
                    d = "right"
                
                elif cell == 5 and prev == "right":
                    d = "up"
                elif cell == 5 and prev == 'down':
                    d = "left"

                elif cell == 6 and prev == "down":
                    d = "right"
                elif cell == 6 and prev == "left":
                    d = "up"

                else:
                    return False

                if d == "right": curr = (r, c+1)
                if d == "left": curr = (r, c-1)
                if d == "up": curr = (r-1, c)
                if d == "down": curr = (r+1, c)

                prev = d
            
            return False

        if start == 4:
            res = run("left")
            visited = set()
            res = res or run("up")
            return res

        return run(prev)

26506