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:

  1. Once you move the ball out of boundary, you cannot move it back.
  2. The length and height of the grid is in range [1,50].
  3. 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的更多相关文章

  1. leetcode 576. Out of Boundary Paths 、688. Knight Probability in Chessboard

    576. Out of Boundary Paths 给你一个棋盘,并放一个东西在一个起始位置,上.下.左.右移动,移动n次,一共有多少种可能移出这个棋盘 https://www.cnblogs.co ...

  2. 【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 ...

  3. 【LeetCode】576. Out of Boundary Paths 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 状态搜索 记忆化搜索 相似题目 参考资料 ...

  4. leetcode 576. Out of Boundary Paths

    leetcode 576 题意大概就是在一个m*n的网格中,在坐标为[i,j]的网格上放一个物体,在规定时间N(t<=N)中,有多少种方法把物体移动出去.物体只能上下左右移动,一次移动一格,移动 ...

  5. 第十一周 Leetcode 576. Out of Boundary Paths (HARD) 计数dp

    Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了 ...

  6. [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 ...

  7. [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 ...

  8. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  9. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

随机推荐

  1. Linux MTD子系统 _从模型分析到Flash驱动模板

    MTD(Memory Technology Device)即常说的Flash等使用存储芯片的存储设备,MTD子系统对应的是块设备驱动框架中的设备驱动层,可以说,MTD就是针对Flash设备设计的标准化 ...

  2. 安装psacct或acct程序包

    监视Linux用户活动 我认为,对每个想密切监视其服务器/系统上用户活动的Linux/Unix系统管理员来说,psacct或acct是优秀的.必需的应用程序之一. psacct或acct程序包提供了用 ...

  3. iwebshop中的增删改查

    <?php class Text extends IController { public function hello() { $this->redirect('hello'); } p ...

  4. C++—动态内存管理之深入探究new和delete

    C++中程序存储空间除栈空间和静态区外,每个程序还拥有一个内存池,这部分内存被称为自由空间(free store)或堆(heap).程序用堆来存储动态分配的对象,即,那些程序运行时分配的对象.动态对象 ...

  5. 高并发场景之RabbitMQ篇

    上次我们介绍了在单机.集群下高并发场景可以选择的一些方案,传送门:高并发场景之一般解决方案 但是也发现了一些问题,比如集群下使用ConcurrentQueue或加锁都不能解决问题,后来采用Redis队 ...

  6. SPOJ-ANTP [组合数学]

    tags:[组合][预处理]题解:关于方程A+C+B=X的正整数解组数.我们用插板法可知,解的组数=在(X-1)个元素中选择两个元素的方案数故答案为:C(x-1,2)+C(x,2)+C(x+1,2)+ ...

  7. robotium问答

    robotium问答   robotium集成instrumentation robotium如何定位控件? search类获取当前所有的view,然后根据类型或者文本去筛选,找到view后获取坐标, ...

  8. POPTEST 测试开发 免费培训课程报名

    poptest是国内唯一一家培养测试开发工程师的培训机构,以学员能胜任自动化测试,性能测试,测试工具开发等工作为目标.如果对课程感兴趣,请大家咨询qq:908821478,咨询电话010-845052 ...

  9. AngularJS1.X学习笔记3-内置模板指令

    前面学习了数据绑定指令,现在开始学习内置模板指令.看起来有点多,目测比较好理解.OK!开始! 一.ng-repeat 1.基本用法 <!DOCTYPE html> <html lan ...

  10. Android 用 camera2 API 自定义相机

    前言 笔者因为项目需要自定义相机,所以了解了一下 Android 关于 camera 这块的 API.Android SDK 21(LOLLIPOP) 开始已经弃用了之前的 Camera 类,提供了 ...