【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 ...
随机推荐
- 基因id
每个物种都有一个对应的Taxonomy ID: 9606 :人类 10090 :小鼠
- java获取当前月第一天和最后一天
获取当前月第一天: /** * 获取当前月第一天 * @param month * @return */ public static String getFirstDayOfMonth(int mon ...
- 4412 RS485
一.485硬件原理 差分对传输数据的原理 IO数据的传输→差分对 rs232传输的距离在15米以下,RS485传输距离是几十米到1000米以上 为什么485可以传输这么远 差分对的机制可以降低电磁场的 ...
- hdu 1007 Quoit Design (经典分治 求最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- 左除与右除的区别--MATLAB
MATLAB 左除与右除的区别 先定义两个矩阵a和矩阵b.如下: /:右除.a/b表示矩阵a乘以矩阵b的逆. \:左除.a\b表示矩阵a的逆乘以b. ./:右除.a./b表示矩阵a中的每个元素除以矩阵 ...
- window安装consul
安装consul 下载包: https://www.consul.io/ 解压 consul_1..2_windows_amd64.zip 复制 consul.exe 到 d:\soft\consul ...
- mysql联合查询sql优化
我们在使用mysql数据库时,经常会使用到mysql的联合查询,联合查询分为内连接和外连接,内连接查询结果是联合的表都存在匹配才会有结果,外连接则根据驱动表是否存在匹配来生成结果集. 这里使用mysq ...
- SQL学习记录:定义(一)
--1.在这里@temp是一个表变量,只有一个批处理中有效,declare @temp table; --2. 如果前面加#就是临时表,可以在tempDB中查看到,它会在最后一个使用它的用户退出后才失 ...
- CPUID
http://en.wikipedia.org/wiki/CPUID CPUID From Wikipedia, the free encyclopedia The CPUID opcode i ...
- 如何使用iText制作中文PDF
1. 下载itextpdf.jar 基础包:http://jaist.dl.sourceforge.net/project/itext/iText/iText5.5.2/itext-5.5.2.zip ...

