TypechoJoeTheme

IT技术分享

统计

[LeetCode 576] Out of Boundary Paths [Java] [Beats : 95.73%]

2017-09-24
/
0 评论
/
791 阅读
/
正在检测是否收录...
09/24

1. Description

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 10^9 + 7.

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

2. Runtime Distribution

3. Submission Details

4. Example

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:

5. Code

private final int MOD = 1000000007;
private int N;
private static int[][] dirs = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

public int findPaths(int m, int n, int N, int i, int j) {

    if (m <= 0 || n <= 0 || N == 0 || i < 0 || j < 0) {
        return 0;
    }

    this.N = N;

    int[][][] dp = new int[m][n][N + 1];
    return DFS(0, i, j, m, n, dp);
}

private int DFS(int step, int i, int j, int m, int n, int[][][] dp) {

    if (i < 0 || i == m || j < 0 || j == n) {
        return 1;
    }

    if (step == N) {
        return 0;
    }

    int remain = N - step;

    if (i - remain >= 0 && i + remain < m && j - remain >= 0 && j + remain < n) {
        return 0;
    }

    if (dp[i][j][step] > 0) {
        return dp[i][j][step];
    }

    for (int k = 0; k < dirs.length; k++) {
        dp[i][j][step] += DFS(step + 1, i + dirs[k][0], j + dirs[k][1], m, n, dp) % MOD;
        dp[i][j][step] %= MOD;
    }
    return dp[i][j][step];
}

6.Test

public class LeetCode0576 {

    private final int MOD = 1000000007;
    private int N;
    private static int[][] dirs = { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } };

    public int findPaths(int m, int n, int N, int i, int j) {

        if (m <= 0 || n <= 0 || N == 0 || i < 0 || j < 0) {
            return 0;
        }

        this.N = N;

        int[][][] dp = new int[m][n][N + 1];
        return DFS(0, i, j, m, n, dp);
    }

    private int DFS(int step, int i, int j, int m, int n, int[][][] dp) {

        if (i < 0 || i == m || j < 0 || j == n) {
            return 1;
        }

        if (step == N) {
            return 0;
        }

        int remain = N - step;

        if (i - remain >= 0 && i + remain < m && j - remain >= 0 && j + remain < n) {
            return 0;
        }

        if (dp[i][j][step] > 0) {
            return dp[i][j][step];
        }

        for (int k = 0; k < dirs.length; k++) {
            dp[i][j][step] += DFS(step + 1, i + dirs[k][0], j + dirs[k][1], m, n, dp) % MOD;
            dp[i][j][step] %= MOD;
        }
        return dp[i][j][step];
    }

    public static void main(String[] args) {
        LeetCode0576 solution = new LeetCode0576();
        System.out.println(solution.findPaths(2, 2, 2, 0, 0));
    }
}
DFS
朗读
赞 · 0
版权属于:

IT技术分享

本文链接:

https://idunso.com/archives/928/(转载时请注明本文出处及文章链接)