<p>You have two types of tiles: a <code>2 x 1</code> domino shape and a tromino shape. You may rotate these shapes.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino.jpg" style="width: 362px; height: 195px;" />
<p>Given an integer n, return <em>the number of ways to tile an</em> <code>2 x n</code> <em>board</em>. Since the answer may be very large, return it <strong>modulo</strong> <code>10<sup>9</sup> + 7</code>.</p>

<p>In a tiling, every square must be covered by a tile. Two tilings are different if and only if there are two 4-directionally adjacent cells on the board such that exactly one of the tilings has both squares occupied by a tile.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/07/15/lc-domino1.jpg" style="width: 500px; height: 226px;" />
<pre>
<strong>Input:</strong> n = 3
<strong>Output:</strong> 5
<strong>Explanation:</strong> The five different ways are show above.
</pre>

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

<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 1
</pre>

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

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

#include <vector>
#include <iostream>
using namespace std;

class Solution {
public:
    int numTilings1(int n, int state, vector<vector<int>>& memoisation) {
        // Base cases

        

        if ((n == 0 || n == 1) && state != 0) {
            return 0;
        }

        if ((n == 0 || n == 1) && state == 0) {
            return 1 ;
        }

        if (n == 2 && state == 0) {
            return 2;
        }

        if (n == 2 && state != 0) {
            return 1;
        }

        if (memoisation[state][n] != -1) {
            return memoisation[state][n];
        }

        const int MOD = 1000000007;
        long long  sum = 0;

        // Calculate for state 0 (nothing missing)
        if (state == 0) {
            sum = (sum + numTilings1(n - 1, 0, memoisation)) ;
            sum = (sum + numTilings1(n - 2, 0, memoisation)) ;
            sum = (sum + numTilings1(n - 1, 1, memoisation)) ;//means that the the top is full 
            sum = (sum + numTilings1(n - 1, 2, memoisation)) ;//means that the botom is full
        }

        // Calculate for state 1 (top is full)
        if (state == 1) {
            sum = (sum + numTilings1(n - 2, 0, memoisation)) ;
            sum = (sum + numTilings1(n - 1, 2, memoisation)) ;
        }

        // Calculate for state 2 (bottom is missing)
        if (state == 2) {
            sum = (sum + numTilings1(n - 2, 0, memoisation)) ;
            sum = (sum + numTilings1(n - 1, 1, memoisation)) ;
        }

        memoisation[state][n] = sum % MOD; // Ensure the result is modulo MOD
        return memoisation[state][n];
    }

    int numTilings(int n) {
        // Memoization table with 3 states (0, 1, 2) and n+1 columns for each n
        vector<vector<int>> memoisation(3, vector<int>(n + 1, -1));
        return numTilings1(n, 0, memoisation);
    }
};

8200