zoukankan      html  css  js  c++  java
  • 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];
        }
  • 相关阅读:
    Cx的治疗
    [HDU2222] Keywords Search
    楼房
    [51Nod1089] 最长回文子串 V2(Manacher算法)
    [洛谷P3375] 【模板】KMP字符串匹配
    [codeVS1204] 寻找子串位置
    [洛谷P1114] “非常男女”计划
    [BZOJ2132] 圈地计划
    [COGS311] Redundant Paths
    [COGS309] [USACO 3.2] 香甜的黄油
  • 原文地址:https://www.cnblogs.com/stAr-1/p/7881429.html
Copyright © 2011-2022 走看看