909. Snakes and Ladders

On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row.  For example, for a 6 x 6 board, the numbers are written as follows:

36 35 34 33 32 31
25 26 27 28 29 30
24 23 22 21 20 19
13 14 15 16 17 18
12 11 10 09 08 07
01 02 03 04 05 06

You start on square 1 of the board (which is always in the last row and first column).  Each move, starting from square x, consists of the following:

  • You choose a destination square S with number x+1x+2x+3x+4x+5, or x+6, provided this number is <= N*N.
  • If S has a snake or ladder, you move to the destination of that snake or ladder.  Otherwise, you move to S.

A board square on row r and column c has a "snake or ladder" if board[r][c] != -1.  The destination of that snake or ladder is board[r][c].

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 continue moving.

Return the least number of moves required to reach square N*N.  If it is not possible, return -1.

Example 1:

Input: [
[-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: 
At 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 (row 3, column 5), 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.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.

Note:

  1. 2 <= board.length = board[0].length <= 20
  2. board[i][j] is between 1 and N*N or is equal to -1.
  3. The board square with number 1 has no snake or ladder.
  4. The board square with number N*N has no snake or ladder.

思路:BFS,注意只有经历了加1-6这个过程才把当前值加到vis变量,否则可能漏掉最优解

class Solution(object):
    def snakesAndLadders(self, a):
        """
        :type board: List[List[int]]
        :rtype: int
        """
        n,m=len(a),len(a[0])
        f=True
        gt=[[0 for _ in range(m)] for _ in range(n)]
        for j in range(n-1,-1,-1):
            i=n-1-j
            t=list(range(m*i+1,m*i+m+1))
            gt[j]=t if f else t[::-1]
            f=not f
        d={}
        for i in range(n):
            for j in range(m):
                d[gt[i][j]]=(i,j)
        
        q,qq=[1],[]
        step=0
        vis=set()
        while q:
            while q:
                x=q.pop()
                vis.add(x)
                if x==n*m: return step
                for t in range(1,7):
                    tt=x+t
                    if tt>n*m: continue
#                    vis.add(tt)
                    i,j=d[tt]
                    if a[i][j]>0:
#                        vis.add(a[i][j])
                        i,j=d[a[i][j]]
                    if gt[i][j] not in vis:
                        qq.append(gt[i][j])
            q,qq=qq,q
            step+=1
        return -1

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值