-
Medium
-
You are given an
n x n
integer matrixboard
where the cells are labeled from1
ton2
in a Boustrophedon style starting from the bottom left of the board (i.e.board[n - 1][0]
) and alternating direction each row.You start on square
1
of the board. In each move, starting from squarecurr
, do the following:- Choose a destination square
next
with a label in the range[curr + 1, min(curr + 6, n2)]
.- This choice simulates the result of a standard 6-sided die roll: i.e., there are always at most 6 destinations, regardless of the size of the board.
- If
next
has a snake or ladder, you must move to the destination of that snake or ladder. Otherwise, you move tonext
. - The game ends when you reach the square
n2
.
A board square on row
r
and columnc
has a snake or ladder ifboard[r][c] != -1
. The destination of that snake or ladder isboard[r][c]
. Squares1
andn2
do not have a snake or ladder.Note that you only take a snake or ladder at most once per move. If the destination to a snake or ladder is the start of another snake or ladder, you do not follow the subsequent snake or ladder.
- For example, suppose the board is
[[-1,4],[-1,3]]
, and on the first move, your destination square is2
. You follow the ladder to square3
, but do not follow the subsequent ladder to4
.
Return the least number of moves required to reach the square
n2
. If it is not possible to reach the square, return-1
. - Choose a destination square
**Example 1:**
![](https://assets.leetcode.com/uploads/2018/09/23/snakes.png)
```
Input: board = [[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,-1,-1,-1,-1,-1],[-1,35,-1,-1,13,-1],[-1,-1,-1,-1,-1,-1],[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation:
In the beginning, you start at square 1 (at row 5, column 0).
You decide to move to square 2 and must take the ladder to square 15.
You then decide to move to square 17 and must take the snake to square 13.
You then decide to move to square 14 and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
This is the lowest possible number of moves to reach the last square, so return 4.
```
The first thing that comes to my mind is DP. Each position on the board comes either from rolling the dice 1~6 or directly from a ladder or snake. So here is my first version of the code.
####This is wrong
class Solution:
def snakesAndLadders(self, board: List[List[int]]) -> int:
n=len(board)
self.short={}
self.seen={}
counter=0
for i in reversed(range(n)):
if (n-i) %2==1:
for j in range(n):
counter+=1
if board[i][j]!=-1:
self.short[counter]=board[i][j]
if (n-i)%2==0:
for j in reversed(range(n)):
counter+=1
if board[i][j]!=-1:
self.short[counter]=board[i][j]
return self.helper(n**2)
def helper(self,position):
if position in self.seen:
return self.seen[position]+1
if position<8:
return 1
mini=min(self.helper(position-1),self.helper(position-2),self.helper(position-3),self.helper(position-4),self.helper(position-5),self.helper(position-6))+1
if position in self.short:
print(position,self.short)
mini=min(mini,self.helper(self.short[position]))
self.seen[position]=mini
return mini
However when I started to run it I realized that "snake" is causing a lot of problem for me and it will create a endless circle since it is pointing from the higher values to the lower values.
class Solution:
def snakesAndLadders(self, board):
n = len(board)
need = {1: 0}
bfs = [1]
for x in bfs:
refuse_steps = False
for i in range(min(n**2, x + 6), x, -1):
a, b = (i - 1) // n, (i - 1) % n
nxt = board[~a][b if a % 2 == 0 else ~b]
if nxt > 0: i = nxt
if i == n * n: return need[x] + 1
if nxt == -1 and refuse_steps: continue
if nxt == -1: refuse_steps = True
if i not in need:
need[i] = need[x] + 1
bfs.append(i)
return -1