【leetcode】909. Snakes and Ladders
题目如下:


解题思路:天坑题,不在于题目多难,而是要理解题意。题目中有两点要特别注意,一是“You choose a destination square S with number x+1, x+2, x+3, x+4, x+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的更多相关文章
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
- 【刷题】【LeetCode】000-十大经典排序算法
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接 000-十大经典排序算法
- 【leetcode】893. Groups of Special-Equivalent Strings
Algorithm [leetcode]893. Groups of Special-Equivalent Strings https://leetcode.com/problems/groups-o ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【leetcode】557. Reverse Words in a String III
Algorithm [leetcode]557. Reverse Words in a String III https://leetcode.com/problems/reverse-words-i ...
随机推荐
- springboot+mybatis搭建web项目
使用idea+springboot+Mybatis搭建一个简单的web项目. 首先新建一个项目: 在这里选择Maven项目也可以,但是IDEA为我们提供了一种更方便快捷的创建方法,即Spring In ...
- CentOS6.8 安装/升级Python2.7.x,并安装最新setuptools、pip、fabric程序总结
最终靠谱的可借鉴文档: 1.python官网 2.http://lovesoo.org/python-fabric-yuan-cheng-zi-dong-bu-shu-jian-jie.html 3. ...
- boost system
boost::system::error_code is the most basic class in Boost.System; it represents operating system-sp ...
- 英语单词custom
custom 来源——xshell快捷键 翻译 n. 习惯,惯例:风俗:海关,关税:经常光顾:[总称](经常性的)顾客 adj. (衣服等)定做的,定制的 高中 | 初中 词源 英语单词custom含 ...
- TypeError:NoneType object is not callable情况下的错误
只用在该视图函数下面加上return ‘值’
- Java Web学习总结(1)Tomcat使用教程
一,简介 Tomcat是一个实现了JAVA EE标准的最小的WEB服务器,是Apache 软件基金会的Jakarta 项目中的一个核心项目,由Apache.Sun 和其他一些公司及个人共同开发而成.因 ...
- 【HDU6662】Acesrc and Travel【树形DP】
题目大意:给你一棵树,每个节点有一个权值,Alice和Bob进行博弈,起点由Alice确定,确定后交替选择下一个点,Alice目标是最终值尽可能大,Bob目标是尽可能小 题解:很明显是树形DP,那么考 ...
- nginx启动、关闭、重启及常用的命令
nginx常用命令 启动:cd /usr/local/nginx/sbin./nginxnginx服务启动后默认的进程号会放在/usr/local/nginx/logs/nginx.pid文件cat ...
- java继承方法覆盖
public class TestB { private void f() { System.out.println("TestB"); } public static void ...
- Process Monitor监控进程操作注册表如何实现?
http://zhidao.baidu.com/link?url=Kqav4qkQSprC5FnpHPOGJvhqvY9fJ9-Vdx9g_SWh4w5VOusdRJo4Vl7qIdrG4LwRJvr ...