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

int mod = 1e9 + 7;
class Solution {
//int dp[55][55][51];
private:
    // int solve(int m,int n,int moves,int x,int y){
    //    if(x ==m || y == n || x==-1 || y == -1)return 1;
    //   if(moves==0){
    //     return 0;
    //   }
    //   if(dp[x][y][moves] != -1)return dp[x][y][moves];
    //   int dx[4] = {-1,1,0,0};
    //   int dy[4] = {0,0,1,-1};
    //   int cnt = 0;
    //   for(int i=0;i<4;i++){
    //     int nx = x + dx[i];
    //     int ny = y + dy[i];
    //     if(nx<=m && nx>=-1 && ny<=n && ny>=-1){
    //       cnt = (0LL + cnt + solve(m,n,moves-1,nx,ny))%mod;
    //     }
    //   }
    //   return dp[x][y][moves] = cnt;
    // }
public:
    int findPaths(int m, int n, int moves, int x, int y) {
        int dp[n+2][m+2][2];
        memset(dp,0,sizeof(dp));
        for(int move = 0;move<=moves;move++){
          for(int i = -1;i<=n;i++){
            for(int j =-1;j<=m;j++){
              if(i == -1 || j == -1 || i == n || j == m){
                dp[i+1][j+1][move%2] = 1;
                continue;
              }
              if(move == 0){
                dp[i+1][j+1][move%2] = 0;
                continue;
              }
              int cnt = 0;
              int dx[4] = {-1,1,0,0};
              int dy[4] = {0,0,1,-1};
              for(int k =0;k<4;k++){
                int nx = i + dx[k];
                int ny = j + dy[k];
                cnt = (0LL + cnt + dp[nx+1][ny+1][(move-1)%2])%mod;
              }
              dp[i+1][j+1][move%2] = cnt;
            }
          }
        }
        return dp[y+1][x+1][moves%2];
    }
};
7791