# Out of Boundary Paths LeetCode Solution

Here, We see Out of Boundary Paths LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.

# List of all LeetCode Solution

## Problem Statement

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

## Out of Boundary Paths LeetCode Solution C++

``````class Solution {
public:
int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
const int M = 1000000000 + 7;
vector<vector<int>> dp(m, vector<int>(n, 0));
dp[startRow][startColumn] = 1;
int count = 0;
for (int moves = 1; moves <= maxMove; moves++) {
vector<vector<int>> temp(m, vector<int>(n, 0));
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == m - 1) count = (count + dp[i][j]) % M;
if (j == n - 1) count = (count + dp[i][j]) % M;
if (i == 0) count = (count + dp[i][j]) % M;
if (j == 0) count = (count + dp[i][j]) % M;
temp[i][j] = (
((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M +
((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M
) % M;
}
}
dp = temp;
}
return count;
}
};```Code language: PHP (php)```

## Out of Boundary Paths LeetCode Solution Java

``````class Solution {
public int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
final int M = 1000000000 + 7;
int[][] dp = new int[m][n];
dp[startRow][startColumn] = 1;
int count = 0;
for (int moves = 1; moves <= maxMove; moves++) {
int[][] temp = new int[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (i == m - 1) count = (count + dp[i][j]) % M;
if (j == n - 1) count = (count + dp[i][j]) % M;
if (i == 0) count = (count + dp[i][j]) % M;
if (j == 0) count = (count + dp[i][j]) % M;
temp[i][j] = (
((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M +
((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M
) % M;
}
}
dp = temp;
}
return count;
}
}```Code language: PHP (php)```

## Out of Boundary Paths LeetCode Solution JavaScript

``````var findPaths = function(m, n, maxMove, startRow, startColumn) {
const M = 1000000000 + 7;
let dp = new Array(m).fill(0).map(() => new Array(n).fill(0));
dp[startRow][startColumn] = 1;
let count = 0;
for (let moves = 1; moves <= maxMove; moves++) {
let temp = new Array(m).fill(0).map(() => new Array(n).fill(0));
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (i === m - 1) count = (count + dp[i][j]) % M;
if (j === n - 1) count = (count + dp[i][j]) % M;
if (i === 0) count = (count + dp[i][j]) % M;
if (j === 0) count = (count + dp[i][j]) % M;
temp[i][j] = (
((i > 0 ? dp[i - 1][j] : 0) + (i < m - 1 ? dp[i + 1][j] : 0)) % M +
((j > 0 ? dp[i][j - 1] : 0) + (j < n - 1 ? dp[i][j + 1] : 0)) % M
) % M;
}
}
dp = temp;
}
return count;
};```Code language: JavaScript (javascript)```

## Out of Boundary Paths LeetCode Solution Python

``````class Solution(object):
def findPaths(self, m, n, maxMove, startRow, startColumn):
M = 1000000000 + 7
dp = [[0] * n for _ in range(m)]
dp[startRow][startColumn] = 1
count = 0
for moves in range(1, maxMove + 1):
temp = [[0] * n for _ in range(m)]
for i in range(m):
for j in range(n):
if i == m - 1:
count = (count + dp[i][j]) % M
if j == n - 1:
count = (count + dp[i][j]) % M
if i == 0:
count = (count + dp[i][j]) % M
if j == 0:
count = (count + dp[i][j]) % M
temp[i][j] = (
((dp[i - 1][j] if i > 0 else 0) + (dp[i + 1][j] if i < m - 1 else 0)) % M +
((dp[i][j - 1] if j > 0 else 0) + (dp[i][j + 1] if j < n - 1 else 0)) % M
) % M
dp = temp
return count```Code language: HTML, XML (xml)```
Scroll to Top