题目如下:

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. 【转】从phpMyAdmin批量导入Excel内容到MySQL(亲测非常简洁有效)

    今天做项目遇到需要用phpMyAdmin批量导入Excel内容到MySQL数据库.分析了我的踏坑经历并且总结一最便捷的一套导入数据的方法,非常实用简洁: 1.修改Excel表的数据,使得Excel中的 ...

  2. [USACO09DEC] Video Game Troubles

    背包DP:有依赖的背包问题 #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...

  3. JS继承 实现方式

    JS中继承方式的实现有多种方法,下面是比较推荐的方法,其它继承方式可做了解: function object(o) { function F() {} F.prototype = o; return ...

  4. 使用mysql应该注意的细节

    一.表及字段的命名规范 1.可读性原则 使用大写和小写来格式化的库对象名字以获得良好的可读性. 例如:使用CustAdress而不是custaddress来提高可读性.(这里注意有些DBMS系统对表名 ...

  5. VB.NET导出Excel 轻松实现Excel的服务器与客户端交换 服务器不安装Office

    说来VB.Net这个也是之前的一个项目中用到的.今天拿来总结下用途,项目需求,不让在服务器安装Office办公软件.这个也是煞费了一顿. 主要的思路就是 在导出的时候,利用DataTable做中间变量 ...

  6. 大数据给IT企业带来攫金市场新机遇

    大数据给IT企业带来攫金市场新机遇 大数据,一个时髦的名词,也是当下热门的业务领域.大数据诱人的利益诉求点之一,即通过大数据能更好地提高效率,更好地有的放矢.一方面,大数据让公司内部更有效地运作:另一 ...

  7. intel instruction 指令速查

    参考:http://ref.x86asm.net/ http://ref.x86asm.net/coder32.html

  8. js的几个特殊的运算符略解

    js运算符的一些特殊应用及使用技巧. 1. 是否包含指定字符: ~ ~"str1".indexOf("str2") 含义为:str1 被查找的字符串 str2 ...

  9. Cocos2d 之FlyBird开发---GameData类

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 现在是大数据的时代,绝大多数的游戏也都离不开游戏数据的控制,简单的就是一般记录游戏的得分情况,高端大气上档次一点的就是记录和保存各方面的游 ...

  10. 插件化框架解读之so 文件加载机制(四)

    阿里P7移动互联网架构师进阶视频(每日更新中)免费学习请点击:https://space.bilibili.com/474380680 提问 本文的结论是跟着 System.loadlibrary() ...