<p>The chess knight has a <strong>unique movement</strong>,&nbsp;it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an <strong>L</strong>). The possible movements of chess knight are shown in this diagram:</p>

<p>A chess knight can move as indicated in the chess diagram below:</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/18/chess.jpg" style="width: 402px; height: 402px;" />
<p>We have a chess knight and a phone pad as shown below, the knight <strong>can only stand on a numeric cell</strong>&nbsp;(i.e. blue cell).</p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/08/18/phone.jpg" style="width: 242px; height: 322px;" />
<p>Given an integer <code>n</code>, return how many distinct phone numbers of length <code>n</code> we can dial.</p>

<p>You are allowed to place the knight <strong>on any numeric cell</strong> initially and then you should perform <code>n - 1</code> jumps to dial a number of length <code>n</code>. All jumps should be <strong>valid</strong> knight jumps.</p>

<p>As the answer may be very large, <strong>return the answer modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> n = 2
<strong>Output:</strong> 20
<strong>Explanation:</strong> All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> n = 3131
<strong>Output:</strong> 136006598
<strong>Explanation:</strong> Please take care of the mod.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= n &lt;= 5000</code></li>
</ul>

class Solution {
public:
    int n;
    int mod = 1000000007;

    // 3D vector for memoization: dp[i][j][len]
    // i, j: current position on the keypad
    // len: current length of the sequence
    vector<vector<vector<int>>> dp;

    int solve(vector<vector<char>> &arr, int i, int j, int len) {
        if (i >= 4 || i < 0 || j >= 3 || j < 0)
            return 0;

        if (arr[i][j] == '*' || arr[i][j] == '#')
            return 0;

        if (len == n)
            return 1;

        // If this state has already been computed, return the stored result
        if (dp[i][j][len] != -1)
            return dp[i][j][len];

        int dx[] = {2, 2, 1, 1, -1, -1, -2, -2};
        int dy[] = {1, -1, 2, -2, 2, -2, 1, -1};
        int ans = 0;

        for (int k = 0; k < 8; k++) {
            int nr = i + dx[k];
            int nc = j + dy[k];

            ans = (ans + solve(arr, nr, nc, len + 1)) % mod;
        }

        // Store the result in the memoization table
        dp[i][j][len] = ans;
        return ans;
    }

    int knightDialer(int _n) {
        n = _n;

        // Initialize the memoization table with -1 (indicating uncomputed states)
        dp = vector<vector<vector<int>>>(4, vector<vector<int>>(3, vector<int>(n + 1, -1)));

        vector<vector<char>> arr(4, vector<char>(3, '0'));
        char ans = '1';

        // Initialize the keypad
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                arr[i][j] = ans++;
            }
        }
        arr[3][0] = '*';
        arr[3][1] = '0';
        arr[3][2] = '#';

        int res = 0;

        // Try starting from every cell on the keypad
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 3; j++) {
                res = (res + solve(arr, i, j, 1)) % mod;
            }
        }

        return res;
    }
};

31877