zoukankan      html  css  js  c++  java
  • 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:

    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 - 1][row - 1][col] + dp[step - 1][row + 1][col] + dp[step - 1][row][col - 1] + dp[step - 1][row][col + 1]

    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 + 1][m][n] = {};
            for(int step = 1; step <= N; step++){
                for(int row = 0; row < m; row++){
                    for(int col = 0; 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 == 0?       1 : dp[step - 1][row - 1][col]) 
                                            + (row == m - 1?    1 : dp[step - 1][row + 1][col])
                                            + (col == 0?        1 : dp[step - 1][row][col - 1])
                                            + (col == n - 1?    1 : dp[step - 1][row][col + 1])) % 1000000007;
                    }
                }
            }
            return dp[N][i][j];
        }
    };
  • 相关阅读:
    IIS调试技术之 Debug Diagnostic (调试诊断)
    其实,你什么都不用怕
    应用程序出现挂死,.NET Runtime at IP 791F7E06 (79140000) with exit code 80131506.
    LR_问题_如何将场景中的用户设置为百分比形式
    LR_问题_脚本运行时提示没有参数化
    LR_问题_无法打开IE浏览器、监视服务器资源
    LR_问题_运行场景时提示scripts you are running in invalid
    收集好用的在线调试工具
    es2015 解构赋值
    shim和polyfill
  • 原文地址:https://www.cnblogs.com/wdw828/p/6823307.html
Copyright © 2011-2022 走看看