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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- php人员权限管理(RBAC)
在说权限管理前,应该先知道权限管理要有哪些功能: (1).用户只能访问,指定的控制器,指定的方法 (2).用户可以存在于多个用户组里 (3).用户组可以选择,指定的控制器,指定的方法 (4).可以添 ...
- pyqt样式表语法笔记(中)
pyqt样式表语法笔记(中) pyqt QSS python 样式表 一.弹窗 在日常的各种桌面软件的使用中,我们都会碰到弹窗.例如注册,登录的时候,会有相应的信息弹窗,这里就以信息收集弹窗为例进行弹 ...
- 浅谈MVC缓存
缓存是将信息放在内存中以避免频繁访问数据库从数据库中提取数据,在系统优化过程中,缓存是比较普遍的优化做法和见效比较快的做法. 对于MVC有Control缓存和Action缓存. 一.Control缓存 ...
- 深度学习实践系列(2)- 搭建notMNIST的深度神经网络
如果你希望系统性的了解神经网络,请参考零基础入门深度学习系列,下面我会粗略的介绍一下本文中实现神经网络需要了解的知识. 什么是深度神经网络? 神经网络包含三层:输入层(X).隐藏层和输出层:f(x) ...
- 给自己的 MAC 添加一个桌面日历
使用 Ubuntu 做自己的办公环境用了将近三年,最近换了新款的 MBP,系统都用的很舒服. 不过 Ubuntu 是在我的 TP W540上部署的,而 W540 + 电源适配太重了(我的电池是9芯的) ...
- 老李分享:《Linux Shell脚本攻略》 要点(二)
老李分享:<Linux Shell脚本攻略> 要点(二) poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课 ...
- Java并发编程:Semaphore、CountDownLatch、CyclicBarrier
首先我们来实现一个功能:当我们启动一个系统的时候需要初始化许多数据,这时候我们可能需要启动很多线程来进行数据的初始化,只有这些系统初始化结束之后才能够启动系统.其实在Java的类库中已经提供了Sema ...
- [转]android4.0.3 修改启动动画和开机声音
本文转自:http://www.cnblogs.com/jqyp/archive/2012/03/07/2383973.html 1. Linux 系统启动,出现Linux小企鹅画面(reboot)( ...
- Hibernate基础学习(六)—Hibernate二级缓存
一.概述 Session的缓存是一块内存空间,在这个内存空间存放了相互关联的Java对象,这个位于Session缓存内的对象也被称为持久化对象,Session负责根据持久化对象的状态来同步更 ...
- JVM年轻代、年老代、永久代
年轻代: HotSpot JVM把年轻代分为了三部分:1个Eden区和2个Survivor区(分别叫From和To),每次新创建对象时,都会分配到Eden区,当Eden区没有足够的空间进行分配时,虚拟 ...