<p>There is an <code>m x n</code> grid with a ball. The ball is initially at the position <code>[startRow, startColumn]</code>. 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 <strong>at most</strong> <code>maxMove</code> moves to the ball.</p>

<p>Given the five integers <code>m</code>, <code>n</code>, <code>maxMove</code>, <code>startRow</code>, <code>startColumn</code>, return the number of paths to move the ball out of the grid boundary. Since the answer can be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_1.png" style="width: 500px; height: 296px;" />
<pre>
<strong>Input:</strong> m = 2, n = 2, maxMove = 2, startRow = 0, startColumn = 0
<strong>Output:</strong> 6
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/04/28/out_of_boundary_paths_2.png" style="width: 500px; height: 293px;" />
<pre>
<strong>Input:</strong> m = 1, n = 3, maxMove = 3, startRow = 0, startColumn = 1
<strong>Output:</strong> 12
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= m, n &lt;= 50</code></li>
	<li><code>0 &lt;= maxMove &lt;= 50</code></li>
	<li><code>0 &lt;= startRow &lt; m</code></li>
	<li><code>0 &lt;= startColumn &lt; n</code></li>
</ul>

class Solution {
    vector<vector<int>> directions = {{-1,0}, {1,0}, {0,-1}, {0,1}};
    const int MOD = 1e9+7;

public:
    int findPaths(int M, int N, int maxMove, int startRow, int startCol) {
        vector<vector<vector<int>>> dp(maxMove + 1, vector<vector<int>>(M + 2, vector<int>(N + 2, 0)));

        for(int move = 0; move <= maxMove; ++move) {
            for(int C = 0; C <= N+1; ++C) {
                dp[move][0][C]     = 1;
                dp[move][M + 1][C] = 1;
            }
        }

        for(int move = 0; move <= maxMove; ++move) {
            for(int R = 0; R <= M+1; ++R) {
                dp[move][R][0]     = 1;
                dp[move][R][N + 1] = 1;
            }
        }

        for(int move = 1; move <= maxMove; ++move) {
            for(int R = 1; R <= M; ++R) {
                for(int C = 1; C <= N; ++C) {
                    int numPaths = 0;
                    for(auto& dir : directions) {
                        int reachRow = R + dir[0];
                        int reachCol = C + dir[1];
                        if(reachRow >= 0 && reachCol >= 0 && reachRow <= M+1 && reachCol <= N+1) {
                            numPaths = ((numPaths % MOD) + dp[move - 1][reachRow][reachCol] % MOD);
                        }
                    }
                    dp[move][R][C] = numPaths % MOD;
                }
            }
        }

        return dp[maxMove][startRow + 1][startCol + 1];
    }
};
11868