Welcome to Subscribe On Youtube

576. Out of Boundary Paths

Description

There is an m x n grid with a ball. The ball is initially at the position [startRow, startColumn]. You are allowed to move the ball to one of the four adjacent cells in the grid (possibly out of the grid crossing the grid boundary). You can apply at most maxMove moves to the ball.

Given the five integers m, n, maxMove, startRow, startColumn, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it modulo 109 + 7.

 

Example 1:

Input: m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
Output: 6

Example 2:

Input: m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
Output: 12

 

Constraints:

  • 1 <= m, n <= 50
  • 0 <= maxMove <= 50
  • 0 <= startRow < m
  • 0 <= startColumn < n

Solutions

  • class Solution {
        private int m;
        private int n;
        private int[][][] f;
        private static final int[] DIRS = {-1, 0, 1, 0, -1};
        private static final int MOD = (int) 1e9 + 7;
    
        public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
            this.m = m;
            this.n = n;
            f = new int[m + 1][n + 1][maxMove + 1];
            for (var a : f) {
                for (var b : a) {
                    Arrays.fill(b, -1);
                }
            }
            return dfs(startRow, startColumn, maxMove);
        }
    
        private int dfs(int i, int j, int k) {
            if (i < 0 || i >= m || j < 0 || j >= n) {
                return 1;
            }
            if (f[i][j][k] != -1) {
                return f[i][j][k];
            }
            if (k == 0) {
                return 0;
            }
            int res = 0;
            for (int t = 0; t < 4; ++t) {
                int x = i + DIRS[t];
                int y = j + DIRS[t + 1];
                res += dfs(x, y, k - 1);
                res %= MOD;
            }
            f[i][j][k] = res;
            return res;
        }
    }
    
  • class Solution {
    public:
        int m;
        int n;
        const int mod = 1e9 + 7;
        int f[51][51][51];
        int dirs[5] = {-1, 0, 1, 0, -1};
    
        int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
            memset(f, 0xff, sizeof(f));
            this->m = m;
            this->n = n;
            return dfs(startRow, startColumn, maxMove);
        }
    
        int dfs(int i, int j, int k) {
            if (i < 0 || i >= m || j < 0 || j >= n) return 1;
            if (f[i][j][k] != -1) return f[i][j][k];
            if (k == 0) return 0;
            int res = 0;
            for (int t = 0; t < 4; ++t) {
                int x = i + dirs[t], y = j + dirs[t + 1];
                res += dfs(x, y, k - 1);
                res %= mod;
            }
            f[i][j][k] = res;
            return res;
        }
    };
    
  • class Solution:
        def findPaths(
            self, m: int, n: int, maxMove: int, startRow: int, startColumn: int
        ) -> int:
            @cache
            def dfs(i, j, k):
                if i < 0 or j < 0 or i >= m or j >= n:
                    return 1
                if k <= 0:
                    return 0
                res = 0
                for a, b in [[-1, 0], [1, 0], [0, 1], [0, -1]]:
                    x, y = i + a, j + b
                    res += dfs(x, y, k - 1)
                    res %= mod
                return res
    
            mod = 10**9 + 7
            return dfs(startRow, startColumn, maxMove)
    
    
  • func findPaths(m int, n int, maxMove int, startRow int, startColumn int) int {
    	f := make([][][]int, m+1)
    	for i := range f {
    		f[i] = make([][]int, n+1)
    		for j := range f[i] {
    			f[i][j] = make([]int, maxMove+1)
    			for k := range f[i][j] {
    				f[i][j][k] = -1
    			}
    		}
    	}
    	var mod int = 1e9 + 7
    	dirs := []int{-1, 0, 1, 0, -1}
    	var dfs func(i, j, k int) int
    	dfs = func(i, j, k int) int {
    		if i < 0 || i >= m || j < 0 || j >= n {
    			return 1
    		}
    		if f[i][j][k] != -1 {
    			return f[i][j][k]
    		}
    		if k == 0 {
    			return 0
    		}
    		res := 0
    		for t := 0; t < 4; t++ {
    			x, y := i+dirs[t], j+dirs[t+1]
    			res += dfs(x, y, k-1)
    			res %= mod
    		}
    		f[i][j][k] = res
    		return res
    	}
    	return dfs(startRow, startColumn, maxMove)
    }
    

All Problems

All Solutions