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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- (21)IO流之对象的序列化和反序列化流ObjectOutputStream和ObjectInputStream
当创建对象时,程序运行时它就会存在,但是程序停止时,对象也就消失了.但是如果希望对象在程序不运行的情况下仍能存在并保存其信息,将会非常有用,对象将被重建并且拥有与程序上次运行时拥有的信息相同.可以使用 ...
- python服务器环境搭建(2)——安装相关软件
在上一篇我们在本地的虚拟服务器上安装好CentOS7后,我们的python web服务.自定义的python service或python脚本需要在服务器上运行,还需要在服务器安装各种相关的软件才行, ...
- MySQL从库忽略某些错误
z熬配置MySQL主从同步的时候常常会因为主库的中SQL语句的错误造成从库的同步出现错误,一旦从库同步出现错误就会造成同步的卡壳影响后续的同步: 可以在从库的配置文件中加入如下的参数,使从库可以自动忽 ...
- 如何给sublime text3安装汉化包?so easy 哦
这是我本人亲身测试过的,肯定有效,没用的话怪我咯. 首先安装package control,然后安装汉化包即可,很简单哦!!! 1.安装package control:打开sublime,使用快捷键: ...
- Java中log4j的使用
前言 距离上一篇文章又过去好长时间了,这段时间一直忙于工作,已经从net彻底转向Java了.工作也慢慢的步入正轨了,自己独自完成了一个小项目,不过工作中遇到了一些问题,还是得到了同学和同事的帮助.本来 ...
- javaScript 基础学习笔记
边看视频和书记得有点杂. 1.插入JS标签 一种是在文档中插入<script></script>标签.另一种是把javaScript代码放在.js文件中.放在head中如. & ...
- 学Java,是自学还是去培训班学习?
现在正在读在校的最后一个学年,想毕业后从事编程,但是感觉自己技术太差,应该是培训呢?还是去找实习?亦或是有更好的途径? 对于 Android 目前的行业趋势,不知道自己该不该坚持?还是转其他行业? 已 ...
- 2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup
2017年陕西省网络空间安全技术大赛——种棵树吧——Writeup 下载下来的zip解压得到两个jpg图片,在Kali中使用binwalk查看文件类型如下图: 有两个发现: 1111.jpg 隐藏了一 ...
- 利用callKit实现电话防骚扰
callKit框架是ios10之后更新的一个框架,代替了原来的CoreTelephony.framework,使用CallKit可以实现电话的拦截 首先创建一个项目之后,创建一个target,选择Ca ...
- Nginx反向代理以及负载均衡配置
项目地址:http://git.oschina.net/miki-long/nginx 前提:最近在研究nginx的用法,在windows上小试了一下,由于windows下不支持nginx缓存配置,所 ...