【leetcode】576. Out of Boundary Paths
题目如下:
There is an m by n grid with a ball. Given the start coordinate (i,j) of the ball, you can move the ball to adjacent cell or cross the grid boundary in four directions (up, down, left, right). However, you can at most move N times. Find out the number of paths to move the ball out of grid boundary. The answer may be very large, return it after mod 109 + 7.
Example 1:
Input: m = 2, n = 2, N = 2, i = 0, j = 0
Output: 6
Explanation:
Example 2:
Input: m = 1, n = 3, N = 3, i = 0, j = 1
Output: 12
Explanation:
Note:
- Once you move the ball out of boundary, you cannot move it back.
- The length and height of the grid is in range [1,50].
- N is in range [0,50].
解题思路:这种题目还是用动态规划吧。记dp[i][j][k] = v 表示从起点开始移动i步到达(j,k)点一共有v种走法,因为每个节都可以从上下左右四个方向移动一步到达该点,很显然有 dp[i][j][k] = dp[i-1][j-1][k] + dp[i-1][j+1][k] + dp[i-1][j][k-1] + dp[i-1][j][k+1] 。最后再累加出所有位于边界的点的走法,假设边界的点的坐标是(x,y),依次判断x == 0, x == m - 1 , y == 0 ,y == n-1四个条件,从这个点走到边界外的走法 = 到达这个点的走法 * 满足条件的个数。
代码如下:
class Solution(object):
def findPaths(self, m, n, N, i, j):
"""
:type m: int
:type n: int
:type N: int
:type i: int
:type j: int
:rtype: int
"""
if N == 0:
return 0
dp = []
for v in range(N):
tl = []
for v in range(m):
tl.append([0] * n)
dp.append(tl)
dp[0][i][j] = 1
for x in range(1,len(dp)):
for y in range(len(dp[x])):
for z in range(len(dp[x][y])):
direction = [(0, 1), (0, -1), (-1, 0), (1, 0)]
for (ny, nz) in direction:
if (y + ny) >= 0 and (y + ny) < m and (z + nz) >= 0 and (z + nz) < n:
dp[x][y][z] += dp[x - 1][y + ny][z + nz]
res = 0
for x in range(len(dp)):
for y in range(len(dp[x])):
for z in range(len(dp[x][y])):
if y == 0:
res += dp[x][y][z]
if z == 0:
res += dp[x][y][z]
if y == m - 1:
res += dp[x][y][z]
if z == n - 1:
res += dp[x][y][z]
#print dp
return res % (pow(10,9)+7)
【leetcode】576. Out of Boundary Paths的更多相关文章
- 【LeetCode】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
- 【leetcode】688. Knight Probability in Chessboard
题目如下: On an NxN chessboard, a knight starts at the r-th row and c-th column and attempts to make exa ...
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 【LeetCode】代码模板,刷题必会
目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...
- 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)
[LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...
- 【LeetCode】113. Path Sum II 解题报告(Python)
[LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...
- 【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 ...
随机推荐
- JavaWeb(五):MVC案例
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它把应用程序分成三个核心模块:模型.视图.控制器,它们各自处理自己的任务.模型是应用程序的主体部分 ...
- leetcode-167周赛-1293-网格中的最短路径
题目描述: 自己的提交:广度优先 O(mn*min(k,m+n)) class Solution: def shortestPath(self, grid, k: int) -> int: vi ...
- centos7安装完成之后必要的配置
一配置yum源 curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo wget ...
- java 序列化机制
package stream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io ...
- 【BZOJ2639】矩形计算(二维普通莫队)
题意:输入一个n*m的矩阵,矩阵的每一个元素都是一个整数,然后有q个询问,每次询问一个子矩阵的权值. 矩阵的权值是这样定义的,对于一个整数x,如果它在该矩阵中出现了p次,那么它给该矩阵的权值就贡献p^ ...
- 转载:IDEA配置SVN及使用
转自:https://blog.csdn.net/zwj1030711290/article/details/80687365 1.安装svn客户端 之前用myEcplise只需要插件,现在IDEA需 ...
- 微博API的申请
https://segmentfault.com/a/1190000012548487
- (转)堆和栈的概念和区别 HeapOutOfMemory和StackOverflow解释
转:https://blog.csdn.net/pt666/article/details/70876410 https://blog.csdn.net/guohan_solft/article/de ...
- linux记事工具:RedNotebook Lifeograph Kontact ThotKeeper
Linux桌面有许多灵活而功能强大的日记工具,如支持标签.加密.多种日志模版和实时搜索.其中的优秀者包括: RedNotebook Lifeograph Kontact ThotKeeper
- phpredis报错信息:protocol error, got 'o' as reply type byte解决方案
今天在前端调用PHP的接口时,有报错信息为:protocol error, got 'o' as reply type byte另外此错误有几率会重现,并不是必现的.十分疑惑,遂百度一下,发现是red ...