<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 {
public:
    const int MOD = 1e9+7;
    int findPaths(int m, int n, int maxMove, int x, int y) {
        if(maxMove == 0)return 0;
        vector<vector<int> > prev(m, vector<int>(n, 0));
        prev[x][y] = 1;

        long long ans = 0;
        if(m == 1 || n == 1){
            ans+=2;
            if((x == 0 || x == m-1) && n == 1)ans++;
            if((y == 0 || y == n-1) && m == 1)ans++;
        }
        else{
            if(x == 0 || x == m-1)ans++;
            if(y == 0 || y == n-1)ans++;
        }


        for(int i = 1; i<maxMove; i++){
            vector<vector<int> > curr(m, vector<int>(n, 0));
            for(int j = 0; j<m; j++){
                for(int k = 0; k<n; k++){
                    // curr[j][k] = prev[j][k];
                    vector<pair<int, int>> del = {{-1,0}, {1, 0}, {0,-1}, {0,1}};
                    for(int l = 0; l<4; l++){
                        int a = del[l].first, b = del[l].second;
                        if(j+a>=0 && j+a<m && k+b>=0 && k+b<n)
                            curr[j][k] = (curr[j][k] + prev[j+a][k+b])%MOD;
                    } 
                    cout<<curr[j][k]<<" \n"[k == n-1];
                }
            }
            
            for(int j = 0; j<m; j++) ans = (ans+curr[j][0]+curr[j][n-1])%MOD;
            for(int k = 0; k<n; k++) ans = (ans+curr[0][k]+curr[m-1][k])%MOD;
            prev = curr;
        }

        
        return ans;
    }
};
27013