<p>In an&nbsp;<code>n*n</code>&nbsp;grid, there is a snake that spans 2 cells and starts moving from the top left corner at <code>(0, 0)</code> and <code>(0, 1)</code>. The grid has empty cells represented by zeros and blocked cells represented by ones. The snake wants to reach the lower right corner at&nbsp;<code>(n-1, n-2)</code>&nbsp;and&nbsp;<code>(n-1, n-1)</code>.</p>

<p>In one move the snake can:</p>

<ul>
	<li>Move one cell to the right&nbsp;if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li>
	<li>Move down one cell&nbsp;if there are no blocked cells there. This move keeps the horizontal/vertical position of the snake as it is.</li>
	<li>Rotate clockwise if it&#39;s in a horizontal position and the two cells under it are both empty. In that case the snake moves from&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r, c+1)</code>&nbsp;to&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r+1, c)</code>.<br />
	<img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-2.png" style="width: 300px; height: 134px;" /></li>
	<li>Rotate counterclockwise&nbsp;if it&#39;s in a vertical position and the two cells to its right are both empty. In that case the snake moves from&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r+1, c)</code>&nbsp;to&nbsp;<code>(r, c)</code>&nbsp;and&nbsp;<code>(r, c+1)</code>.<br />
	<img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image-1.png" style="width: 300px; height: 121px;" /></li>
</ul>

<p>Return the minimum number of moves to reach the target.</p>

<p>If there is no way to reach the target, return&nbsp;<code>-1</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<p><strong><img alt="" src="https://assets.leetcode.com/uploads/2019/09/24/image.png" style="width: 400px; height: 439px;" /></strong></p>

<pre>
<strong>Input:</strong> grid = [[0,0,0,0,0,1],
               [1,1,0,0,1,0],
&nbsp;              [0,0,0,0,1,1],
&nbsp;              [0,0,1,0,1,0],
&nbsp;              [0,1,1,0,0,0],
&nbsp;              [0,1,1,0,0,0]]
<strong>Output:</strong> 11
<strong>Explanation:
</strong>One possible solution is [right, right, rotate clockwise, right, down, down, down, down, rotate counterclockwise, right, down].
</pre>

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

<pre>
<strong>Input:</strong> grid = [[0,0,1,1,1,1],
&nbsp;              [0,0,0,0,1,1],
&nbsp;              [1,1,0,0,0,1],
&nbsp;              [1,1,1,0,0,1],
&nbsp;              [1,1,1,0,0,1],
&nbsp;              [1,1,1,0,0,0]]
<strong>Output:</strong> 9
</pre>

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

<ul>
	<li><code>2 &lt;= n &lt;= 100</code></li>
	<li><code>0 &lt;= grid[i][j] &lt;= 1</code></li>
	<li>It is guaranteed that the snake starts at empty cells.</li>
</ul>

from collections import deque

class Solution:
    def minimumMoves(self, grid: List[List[int]]) -> int:
        inf = 10000
        N = len(grid)
        Hnodes = [[inf for col in range(N-1)] for row in range(N)]
        Vnodes = [[inf for col in range(N)] for row in range(N-1)]
        nodeQueue = deque()

        # Starting position
        Hnodes[0][0] = 0
        nodeQueue.append((0, 0, 0)) # row, col, orientation

        while len(nodeQueue) > 0:
            node = nodeQueue.popleft()
            row = node[0]
            col = node[1]
            orientation = node[2]
            # horizontal case
            if orientation == 0:
                minMoves = Hnodes[row][col]
                # move right case
                if col + 2 < N and grid[row][col + 2] == 0 and Hnodes[row][col + 1] == inf:
                    Hnodes[row][col + 1] = minMoves + 1
                    nodeQueue.append((row, col + 1, 0))
                # move down case or rotate clockwise case
                if row + 1 < N and grid[row + 1][col] == 0 and grid[row + 1][col + 1] == 0:
                    # move down
                    if Hnodes[row + 1][col] == inf:
                        Hnodes[row + 1][col] = minMoves + 1
                        nodeQueue.append((row + 1, col, 0))
                    # rotate clockwise
                    if Vnodes[row][col] == inf:
                        Vnodes[row][col] = minMoves + 1
                        nodeQueue.append((row, col, 1))
            # vertical case
            else:
                minMoves = Vnodes[row][col]
                # move down case
                if row + 2 < N and grid[row + 2][col] == 0 and Vnodes[row + 1][col] == inf:
                    Vnodes[row + 1][col] = minMoves + 1
                    nodeQueue.append((row + 1, col, 1))
                # move right or rotate counter clockwise case
                if col + 1 < N and grid[row][col + 1] == 0 and grid[row + 1][col + 1] == 0:
                    # move right
                    if Vnodes[row][col + 1] == inf:
                        Vnodes[row][col + 1] = minMoves + 1
                        nodeQueue.append((row, col + 1, 1))
                    # rotate counter clockwise
                    if Hnodes[row][col] == inf:
                        Hnodes[row][col] = minMoves + 1
                        nodeQueue.append((row, col, 0))

        minDist = Hnodes[N - 1][N - 2]
        return minDist if minDist < inf else -1
17100