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

class Solution {
public:
    int numTilings(int n) {
        map<pair<int, int>, int> dp;
        return dfsCache(0, n, true, true, dp);
    }

    int dfsCache(int i, int n, bool t1, bool t2, map<pair<int, int>, int>& dp) {
        int mod = (int) 1e9 + 7;
        if (i == n) {
            return 1;
        }
        int state = makeState(t1, t2);
        if (dp.find({i, state}) != dp.end()) {
            return dp[{i, state}];
        }
        bool t3 = true, t4 = true;
        if (i + 1 == n) {
            t3 = false;
            t4 = false;
        }
        long long count = 0;
        // lets start putting tiles
        if (t1 && t2 && t3) {
            // Tromino
            count += dfsCache(i + 1, n, false, true, dp);
        }
        if (t1 && t2 && t4) {
            // Tromino
            count += dfsCache(i + 1, n, true, false, dp);
        }
        if (t1 && !t2 && t3 && t4) {
            // Tromino
            count += dfsCache(i + 1, n, false, false, dp);
        }
        if (!t1 && t2 && t3 && t4) {
            // Tromino
            count += dfsCache(i + 1, n, false, false, dp);
        }
        if (t1 && t2) {
            // one vertical domino
            count += dfsCache(i + 1, n, true, true, dp);
        }
        if (t1 && t2 && t3 && t4) {
            // two horizontal domino
            count += dfsCache(i + 1, n, false, false, dp);
        }
        if (t1 && !t2 && t3) {
            // vertical domino at top
            count += dfsCache(i + 1, n, false, true, dp);
        }
        if (!t1 && t2 && t4) {
            // vertical domino at bottom
            count += dfsCache(i + 1, n, true, false, dp);
        }
        if (!t1 && !t2) {
            // place nothing
            count += dfsCache(i + 1, n, true, true, dp);
        }
        dp[{i, state}] = count % mod;
        return dp[{i, state}];
    }

    int makeState(bool t1, bool t2) {
        if (t1 && t2) {
            // both cells are not occupied
            return 0;
        }
        if (!t1 && t2) {
            return 1;
        }
        if (t1 && !t2) {
            return 2;
        }
        // if both cells are occupied
        return 3;
    }
};
15500