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

#include <unordered_map>

class Solution {
public:
static constexpr int wrapval = (1'000'000'000 + 7);
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
        
        int ans = 0;
        for(int move_ceil = 1; move_ceil <= maxMove; move_ceil++) {
            int x = startColumn;
            int y = startRow;

            int path_count = find_path(move_ceil, x, y, m, n);
            ans += path_count;
            ans %= wrapval;
        }

        return ans;
    }

    int find_path(int count, int x, int y, int m, int n) {
        string key = to_string(count) + "_" + to_string(x) + "_" + to_string(y);
        //cout << key << endl;
        if (count_x_y_paths.find(key) != count_x_y_paths.end()) {
            //cout << "   | " << count_x_y_paths[key] << endl;
            return count_x_y_paths[key] % wrapval;
        }
        
        bool oob = (x >= n || x < 0 || y >= m || y < 0);
        if (count == 0 && oob) {
            //cout << "debug1" << endl;
            return 1;
        } else if (count == 0 || oob) {
            //cout << "debug2" << endl;
            return 0;
        }

        int path_count = 0;
        path_count += find_path(count-1, x+1, y, m, n);
        path_count %= wrapval;
        path_count += find_path(count-1, x-1, y, m, n);
        path_count %= wrapval;
        path_count += find_path(count-1, x, y+1, m, n);
        path_count %= wrapval;
        path_count += find_path(count-1, x, y-1, m, n);
        path_count %= wrapval;
        
        count_x_y_paths[key] = path_count;
        return path_count;
    }

    std::unordered_map<string, int> count_x_y_paths = {};
};
28178