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

template<typename Tuple>
struct TupleHash {
    std::size_t operator()(const Tuple& v) const {
        return this->hash_impl(v, std::make_index_sequence<std::tuple_size_v<Tuple>>{});
    }

private:
    template<typename... T, std::size_t... Ints>
    std::size_t hash_impl(
        const std::tuple<T...>& v,
        std::integer_sequence<std::size_t, Ints...>
    ) const {
        std::size_t result = 0;
        combine_hash(result, std::get<Ints>(v)...);
        return result;
    }

    void combine_hash(std::size_t& /*hash*/) const {}

    template<typename Arg1, typename... OtherArgs>
    void combine_hash(std::size_t& result, const Arg1& arg1, const OtherArgs&... others) const {
        static constexpr int magic = 0x9e3779b9;
        result ^= std::hash<Arg1>()(arg1) + magic + (result << 6) + (result >> 2);
        combine_hash(result, others...);
    }
};

class Solution {
public:
    int findPaths(int m, int n, int maxMove, int startRow, int startColumn) {
        return solve_rec(m, n, startRow, startColumn, maxMove);
    }

private:
    static constexpr pair<int, int> kDirections[] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
    unordered_map<tuple<int, int, int>, int, TupleHash<tuple<int, int, int>>> memo_;

    int solve_rec(int rows, int cols, int r, int c, int moves_left) {
        if (r < 0 || c < 0 || r >= rows || c >= cols)
            return 1;

        if (moves_left <= 0)
            return 0;

        if (auto it = memo_.find({r, c, moves_left}); it != memo_.end())
            return it->second;

        int path_cnt = 0;
        for (const auto [dr, dc]: kDirections)
            path_cnt = (path_cnt + solve_rec(rows, cols, r + dr, c + dc, moves_left - 1)) % 1000000007;

        memo_.insert({{r, c, moves_left}, path_cnt});

        return path_cnt;
    }
};

21188