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

就是从一个地点出发,N步之内出范围的路径有哪些,格子可以重复走,所以用步数来限制多次走来是不同路径

思路是用dp[k][r][jc代表第k步从r,c出发的路径数,动态方程:

dp[k][r][c] = (((r==0)?1:dp[k-1][r-1][c]%num)+((r==m-1)?1:dp[k-1][r+1][c]%num)+((c==0)?1:dp[k-1][r][c-1]%num)+((c==n-1)?1:dp[k-1][r][c+1]%num))%num;
public int findPaths(int m, int n, int N, int i, int j) {
        int num = 1000000007;
        long[][][] dp = new long[N+1][m][n];
        for (int k = 1;k < N+1;k++)
        {
            for (int r = 0; r < m; r++) {
                for (int c = 0; c < n; c++) {
                    dp[k][r][c] = (((r==0)?1:dp[k-1][r-1][c]%num)+((r==m-1)?1:dp[k-1][r+1][c]%num)+((c==0)?1:dp[k-1][r][c-1]%num)+((c==n-1)?1:dp[k-1][r][c+1]%num))%num;
                }
            }
        }
        return (int)dp[N][i][j];
    }
时间: 2024-08-30 01:09:36

576. Out of Boundary Paths的相关文章

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

Leetcode 576 给定一个二维平面, 一个球在初始位置(i,j)每次可以转移到上下左右的一格. 问在N次转移内,有多少种路径可以转移出边境. dp[i][j][k]为 在点(i,j) 已经走了k步的累积路径数. 最后答案就是边境点且k<=N-1之和. 转移方程是显而易见的. const int MOD=1000000007; class Solution { public: int dp[50][50][50]; int findPaths(int m, int n, int N, in

leetcode 576. Out of Boundary Paths

https://leetcode.com/problems/out-of-boundary-paths/#/description 题意大概就是在一个m*n的网格中,在坐标为[i,j]的网格上放一个物体,在规定时间N(t<=N)中,有多少种方法把物体移动出去.物体只能上下左右移动,一次移动一格,移动一次为一个单位时间. 求总的个数,并且每个N都是来自四个方向的N-1之和.很明显用dp做的.还是比较经典的一个dp题把.. 1 class Solution { 2 public: 3 int fin

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】动态规划(上篇共75题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [5] Longest Palindromic Substring 给一个字符串,需要返回最长回文子串 解法:dp[i][j] 表示 s[i..j] 是否是回文串,转移方程是 dp[i][j] = 1 (if dp[i+1][j-1] = 1 && s[i] == s[j]),初始化条件是 if (s[i] == s[j] && (i == j

【LeetCode】深搜DFS(共85题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [104]Maximum Depth of Binary Tree [105]Construct Binary Tree from Preorder and Inorder

Drawing Shapes Using B&#233;zier Paths

Drawing Shapes Using Bézier Paths In iOS 3.2 and later, you can use the UIBezierPath class to create vector-based paths. The UIBezierPath class is an Objective-C wrapper for the path-related features in the Core Graphics framework. You can use this c

lua协程一则报错解决“attempt to yield across metamethod/C-call boundary”

问题 attempt to yield across metamethod/C-call boundary 需求跟如下帖子中描述一致: http://bbs.chinaunix.net/forum.php?mod=viewthread&action=printable&tid=4065715 模拟一个场景,在C中创建出coroutine来执行Lua脚本,并且提供C API给Lua使用,当某些操作可能会阻塞时(如网络I/O),C函数中执行yield将协程切换出去,然后未来的某个时刻,如果条件

leetcode笔记:Unique Paths

一. 题目描述 A robot is located at the top-left corner of a m n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish

LeetCode --- 62. Unique Paths

题目链接:Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below). The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (ma