576. Out of Boundary Paths
Problem statement:
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].
Analysis:
This question is the last one of leetcode weekly contest 31. Initially, it is tagged with medium, and then adjusted to hard today.
They mentioned a position in a two dimension board and at most N step to move and count the numbers to get out of boundary. Obviously, DP.
My first solution:
Start from (i, j), initialize all the element in the row i and col and j compared their value with N.
Do four direction dynamic programming, however, it ignored one fact that the value of one cell can come from all four directions except boundary.
The answer is wrong.
Solution:
This solution is quite simple, we have m * n board and N step to move, it is a 3 dimension DP.
The initialization status: dp[0][0 ... m -1][0 ... n - 1] is 0. means the step is 0, all value is 0.
Current value only comes from four directions of last move or 1 if it is boundary.
DP formula is:
dp[step][row][col] = dp[step - ][row - ][col] + dp[step - ][row + ][col] + dp[step - ][row][col - ] + dp[step - ][row][col + ]
we calculate the value of this three dimension matrix and return the value of dp[N][i][j].
The time complexity is O(N * m * n), space complexity is O((N + 1) * m * n)
class Solution {
public:
int findPaths(int m, int n, int N, int i, int j) {
unsigned int dp[N + ][m][n] = {};
for(int step = ; step <= N; step++){
for(int row = ; row < m; row++){
for(int col = ; col < n; col++){
// the value come from four directoion
// if one value comes from boundary: 1
// dp[step - 1][row - 1][col]
// + dp[step - 1][row + 1][col]
// + dp[step - 1][row][col - 1]
// + dp[step - 1][row][col + 1]
dp[step][row][col] = ((row == ? : dp[step - ][row - ][col])
+ (row == m - ? : dp[step - ][row + ][col])
+ (col == ? : dp[step - ][row][col - ])
+ (col == n - ? : dp[step - ][row][col + ])) % ;
}
}
}
return dp[N][i][j];
}
};
576. Out of Boundary Paths的更多相关文章
- leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard
576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...
- 【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 mov ...
- 【LeetCode】576. Out of Boundary Paths 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...
- leetcode 576. Out of Boundary Paths
leetcode 576 题意大概就是在一个m*n的网格中,在坐标为[i,j]的网格上放一个物体,在规定时间N(t<=N)中,有多少种方法把物体移动出去.物体只能上下左右移动,一次移动一格,移动 ...
- 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp
Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...
- [LeetCode] 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 ...
- [Swift]LeetCode576. 出界的路径数 | 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 ...
- leetcode bugfree note
463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- 纪念一下java学习之第一个get请求。
环境,变量及工具: 1.java jdk1.8.X 2.tomcat 8.5.11 3. 环境变量: CATALINA_HOME: tomcat安装地址 JAVA_HOME: JDK安装地址 ...
- 我所理解的javascript中函数的作用域和作用域链
本文为原创,转载请注明出处: cnzt 文章:cnzt-p 写在前面 一周木有更新了,今天终于攻克了自行车难关,非常开心,特意来一更~ (那些捂嘴偷笑的人我看到你们了快把嘴闭上我会假装没看 ...
- poptest老李谈分布式与集群
poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...
- 从面试小白走向master
腾讯2017春招(实习生招聘)在线笔试知识点总结: 1.栈与队列(用队列实现栈) 2.排序算法(最坏情况下时间复杂度) 3.TCP协议(3次
- 1102: 零起点学算法09——继续练习简单的输入和计算(a-b)
1102: 零起点学算法09--继续练习简单的输入和计算(a-b) Time Limit: 1 Sec Memory Limit: 520 MB 64bit IO Format: %lldSub ...
- How To Use ggplot2
0. Preparation and Introduction ggplot2是R中新颖的数据可视化包,这得益于Leland Wilkinson在他的著作<The Grammar of Grap ...
- [Python]peewee使用经验
peewee 使用经验 本文使用案例是基于 python2.7 实现 以下内容均为个人使用 peewee 的经验和遇到的坑,不会涉及过多的基本操作.所以,没有使用过 peewee,可以先阅读文档 正确 ...
- kindeditor修改图片上传路径-使用webapi上传图片到图片服务器
kindeditor是一个非常好用的富文本编辑器,它的简单使用我就不再介绍了. 在这里我着重介绍一些使用kindeditor修改图片上传路径并通过webapi上传图片到图片服务器的方案. 因为我使用的 ...
- 【Hololens】微软Hololens虚拟现实视频集
混合虚拟现实(Hololens眼镜) 微软还是混合虚拟现实的行业领导者,Hololens眼镜在很多行业有了令人印象深刻的应用和演示.譬如: Hololens中文宣传片: https://pan.bai ...
- 读书笔记 effective c++ Item 49 理解new-handler的行为
1. new-handler介绍 当操作符new不能满足内存分配请求的时候,它就会抛出异常.很久之前,它会返回一个null指针,一些旧的编译器仍然会这么做.你仍然会看到这种旧行为,但是我会把关于它的讨 ...