题目如下:

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:

  1. Once you move the ball out of boundary, you cannot move it back.
  2. The length and height of the grid is in range [1,50].
  3. 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的更多相关文章

  1. 【LeetCode】576. Out of Boundary Paths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...

  2. 【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 ...

  3. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

  4. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  5. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  6. 【LeetCode】113. Path Sum II 解题报告(Python)

    [LeetCode]113. Path Sum II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fu ...

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

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

  8. 【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 ...

  9. 53. Maximum Subarray【leetcode】

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

随机推荐

  1. List和Tuple的中的method对比

  2. tensorflow函数介绍(1)

    tensorflow中的tensor表示一种数据结构,而flow则表现为一种计算模型,两者合起来就是通过计算图的形式来进行计算表述,其每个计算都是计算图上的一个节点,节点间的边表示了计算之间的依赖关系 ...

  3. Word中页码及目录、参考文献的制做方法

    1.页码从正文开始 1.要想从哪里显示第一页,就在这页的前一页的最后一行最后的地方,插入分隔符---下一页2.然后在你想显示第一页的那一页双击页脚处,点击取消掉“链接到前一条页眉”.(这是为了取消原来 ...

  4. textarea 根据光标位置添加内容

    // 获取焦点 let txt = document.getElementById("countRule"); let temp = txt.value; txt.focus(); ...

  5. junit中配置log4j日志

    在对项目单元测试的时候,有时候会使用log4j来打印日志,这个时候需要对log4j进行配置. 声明和继承SpringJUnit4ClassRunner类,在这个类中对log4j的配置文件进行加载. p ...

  6. springboot 导出excel

    依赖 <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</ ...

  7. 119、TensorFlow如何创建计算图

    #Dataflow是并行化编程常用的方式 #当使用TensorFlow执行你的程序的时候有以下几个优点 #1.并行化 .通过声明的边来代表操作之间的依赖 # 对系统来说确定可以并行化的操作是比较容易的 ...

  8. sql server中实现mysql的find_in_set函数和group_concat类似功能的讲解

    charindex(','   +  ' test '+  ','   ,   ',' + test2+ ',')>0 灵活运用 SQL SERVER FOR XML PATH FOR XML ...

  9. Unity获取Android和iOS手机系统电量及网络状况

    最开始考虑使用中间静态链接库来调用手机系统自带的API,但是在研究的过程中发现Android系统将电量等信息记录在了固定的文件中,所以只需要在C#中直接读取就可以而不需要中间库. a.Android版 ...

  10. g++ 之 -m64选项

    今天编译之前的项目,竟然报了下面的错误 usr/bin/ld: i386 architecture of input file `./proxycpp/soapRemoteDiscoveryBindi ...