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们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- JQ万能轮播图
lunbotu.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- [LeetCode] 01 Matrix 题解
题意 # 思路 我一开始的时候想的是嘴 # 实现 ```cpp // // include "../PreLoad.h" class Solution { public: /** ...
- 常见端口、端口查询及TCP状态
查看电脑端口的开放情况命令:cmd——netstat -a -n -a:显示所有连接和监听端口:-n:以数字形式显示地址和端口号 “本地地址”指本地IP地址及其正在使用的端口号,“外部地址”指连接某端 ...
- Android开发遇到短信备份失败
今天做了一个有关ContentProvider的短信备份的小案例,遇到短信备份失败,费了一番周折后终于找到了问题所在 该案例是将短信写到一个xml文件然后保存在手机存储中实现短信的备份功能,关键实现代 ...
- Javascript:面试经典套路-查重(reduce)
今天在偶然间查看到了一段代码,代码使用了很短的篇幅完成了字符串统计相同字符次数这个经典面试题,其中用到了reduce这个方法,网上查了查,没有查到什么有价值的东西,导致浪费了我一些时间才看懂,现将我的 ...
- PHP和js实时倒计时
<?php //这是t.php页面 header('content-type:text/html;charset=utf-8'); date_default_timezone_set('PRC' ...
- webpack2.x基础属性讲解(一)
webpack作为构建工具平时作为前端作为优化.模块编程.和分片打包的重要组成部分,大家可能并不陌生,如果没有时刻的去关注文档,那么大家可能不太清楚webpack已经默默然的升级到2.x了,对比1 ...
- AngularJS1.X学习笔记5-加强版的表单
大家愚人节快乐!表单是沟通服务器端和客户端的桥梁,为数据从客户端传到服务端打开了一扇大门.AngularJS增加了一些新特性,使我们能够更加方便的操作表单.OK!开始学习!学习使我快乐. 一.双向数据 ...
- js遍历(获取)ul中的li
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8&qu ...
- CSS如何实现圆角的outline效果?
一.首先,outline是个很牛逼的东西 温故而知鑫,10年的时候写过一篇可用性方面的文章:“页面可用性之outline轮廓外框的一些研究”,还算挺有用的:3年之后,也就是13年,介绍了个没什么使用价 ...