题目如下:

解题思路:天坑题,不在于题目多难,而是要理解题意。题目中有两点要特别注意,一是“You choose a destination square S with number x+1x+2x+3x+4x+5, or x+6, provided this number is <= N*N.” 这里最大可以移动的x+6中的6真的就是数字6啊,不是例子中的N=6的6,可以理解成是掷骰子。二是“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. ” 这里的意思是不能连续坐梯子,例如如果一个数字是通过梯子到达的,那么即使这个数字本身有梯子,也不能继续坐了,必须移动x+1~x+6中的某一步。理解了题意后,其实这题就非常简单了,DFS或者BFS都行。

代码如下:

class Solution(object):
def getLadderDestination(self,v,N):
for i in range(1,N+1):
if v <= i*N:
break
r = N-i
if (i) % 2 == 1:
c = v - ((i-1)*N + 1)
else:
c = i*N - v
return (r,c) def snakesAndLadders(self, board):
"""
:type board: List[List[int]]
:rtype: int
"""
N = len(board)
#endPosition = None
if N % 2 == 0:
endPosition = (0,0)
else:
endPosition = (0,N-1)
visit = [[N*N+1] * N for i in range(N)]
queue = [(1,0)]
visit[N-1][0] = 0
while len(queue) > 0:
v,step = queue.pop(0)
#x,y = self.getLadderDestination(v,N)
#visit[x][y] = min(visit[x][y],step)
for i in range(v+1,min(N*N,v+6)+1):
x,y = self.getLadderDestination(i,N)
if board[x][y] > 0:
ladder_x, ladder_y = self.getLadderDestination(board[x][y], N)
if board[ladder_x][ladder_y] == 44:
pass
if visit[ladder_x][ladder_y] > step + 1:
queue.append((board[x][y], step + 1))
visit[ladder_x][ladder_y] = min(step + 1, visit[ladder_x][ladder_y])
elif visit[x][y] > step+1:
queue.append((i, step + 1))
visit[x][y] = min(visit[x][y],step+1)
#print visit
return visit[endPosition[0]][endPosition[1]] if visit[endPosition[0]][endPosition[1]] != N*N+1 else -1

【leetcode】909. Snakes and Ladders的更多相关文章

  1. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  2. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  3. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  4. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  5. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  6. 【刷题】【LeetCode】000-十大经典排序算法

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法

  7. 【leetcode】893. Groups of Special-Equivalent Strings

    Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...

  8. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  9. 【leetcode】557. Reverse Words in a String III

    Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...

随机推荐

  1. C#基础提升系列——C#集合

    C#集合 有两种主要的集合类型:泛型集合和非泛型集合. 泛型集合被添加在 .NET Framework 2.0 中,并提供编译时类型安全的集合. 因此,泛型集合通常能提供更好的性能. 构造泛型集合时, ...

  2. 25-Node.js学习笔记-express-app.locals对象

    app.locals对象 将变量设置到app.locals对象下面,这个数据在所有的模板中都可以获取到 app.locals.users=[{ name:'柠檬不酸', age:20 },{ name ...

  3. js image转canvas不显示

    今天在项目开发中遇到了image转canvas不显示的问题,最后翻了不少资料才发现问题出现在图片加载上 如果你的代码是这样的,那么不显示的原因就是img没有加载完成 function convertI ...

  4. IntelliJ IDEA设置maven

    1.更改默认的maven仓库 2.手动更新maven 项目——也就是下载依赖的jar包 3. 不想每次手动更新,设置IDEA自动更新mav项目,下载jar包

  5. delphi 控件背景透明代码

    procedure DrawParentBackground(Control: TControl; DC: HDC; R: PRect = nil; bDrawErasebkgnd: Boolean ...

  6. loadrunner(预测系统行为和性能的负载测试工具)

    LoadRunner,是一种预测系统行为和性能的负载测试工具.通过以模拟上千万用户实施并发负载及实时性能监测的方式来确认和查找问题,LoadRunner能够对整个企业架构进行测试.企业使用LoadRu ...

  7. Android实战技巧:Dialog (转)

    转:http://blog.csdn.net/hitlion2008/article/details/7567549#t0 Dialog是任何系统都必须有的一个控件,作为辅助窗口,用于显示一些消息,或 ...

  8. About Intel® Processor Numbers

    http://www.intel.com/content/www/us/en/processors/processor-numbers.html About Intel® Processor Numb ...

  9. xcode安装pod

    参考了: https://blog.csdn.net/AugustDY/article/details/92078639 输入 ruby -v ,查看当前电脑的ruby版本,如果已经安装了ruby环境 ...

  10. CSDN如何转载别人的博客

    前言   对于喜欢逛CSDN的人来说,看别人的博客确实能够对自己有不小的提高,有时候看到特别好的博客想转载下载,但是不能一个字一个字的敲了,这时候我们就想快速转载别人的博客,把别人的博客移到自己的空间 ...