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

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast", "inline", "ffast-math", "unroll-loops", "no-stack-protector")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native", "f16c")
static const auto DPSolver = []()
{ std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); std::cout.tie(nullptr); return 'c'; }();
int mod = 1e9 + 7;
class Solution {
public:
    Solution(){
        DPSolver; 
    }
    bool isOutside(int i, int j, int m, int n)
    {
        return i < 0 || j < 0 || i >= m || j >= n;
    }

    map<pair<pair<int,int>, int>, int> memo; 
    int findPaths(const int m,const int n, int maxMove, int startRow, int startColumn)
    {
        //?  make it efficient 
        auto it = memo.find({{startRow, startColumn},maxMove}); 
        if (it!= memo.end())
            return it->second; 
        //? make it work
        // base cases
        if (isOutside(startRow, startColumn, m, n)){
            memo[{{startRow, startColumn}, maxMove}] = 1;
            return 1;}
        
        else if (maxMove == 0)
            return 0; 


        // recursive case
        int up = findPaths(m, n, maxMove - 1, startRow - 1, startColumn) % mod;
        int down = findPaths(m, n, maxMove - 1, startRow + 1, startColumn) % mod;
        int left = findPaths(m, n, maxMove - 1, startRow , startColumn -1) % mod;
        int right = findPaths(m, n, maxMove - 1, startRow , startColumn+1) % mod;

        memo[{{startRow, startColumn}, maxMove}] = (((((up + down)%mod) + left)%mod) + right) %mod; 
        return (((((up + down)%mod) + left)%mod) + right) %mod; 
    }
};
22936