<p>You have a <code>grid</code> of size <code>n x 3</code> and you want to paint each cell of the grid with exactly one of the three colors: <strong>Red</strong>, <strong>Yellow,</strong> or <strong>Green</strong> while making sure that no two adjacent cells have the same color (i.e., no two cells that share vertical or horizontal sides have the same color).</p>

<p>Given <code>n</code> the number of rows of the grid, return <em>the number of ways</em> you can paint this <code>grid</code>. As the answer may grow large, the answer <strong>must be</strong> computed modulo <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/2020/03/26/e1.png" style="width: 400px; height: 257px;" />
<pre>
<strong>Input:</strong> n = 1
<strong>Output:</strong> 12
<strong>Explanation:</strong> There are 12 possible way to paint the grid as shown.
</pre>

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

<pre>
<strong>Input:</strong> n = 5000
<strong>Output:</strong> 30228214
</pre>

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

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

class Solution {
public:
    int numOfWays(int n) {
        auto rowAbove = std::make_tuple(Color::NONE, Color::NONE, Color::NONE);
        auto cache = std::unordered_map<CacheKey, int>();
        return Dp(n, rowAbove, cache);
    }
    
private:
    using CacheKey = std::uint32_t;

    enum class Color : std::uint8_t {
        NONE = 0,
        GREEN = 1,
        RED = 2,
        YELLOW = 3,
    };

    int Dp(int n, const std::tuple<Color, Color, Color> rowAbove, std::unordered_map<CacheKey, int>& cache) const {
        if (n == 0) {
            return 1;
        }
        
        auto cacheKey = MakeCacheKey(rowAbove, n);
        if (cache.find(cacheKey) != cache.cend()) {
            return cache[cacheKey];
        }
        
        int countTot = 0;
        
        for (const auto row : ALL_POSSIBLE_ROWS) {
            if (!IsValidRow(row, rowAbove)) {
                continue;
            }
            
            countTot = Sum(countTot, Dp(n-1, row, cache));
        }
        
        cache[cacheKey] = countTot;
        return countTot;
    }

    CacheKey MakeCacheKey(const std::tuple<Color, Color, Color> rowAbove, int n) const {
        auto key = static_cast<CacheKey>(n);
        
        key <<= 2;
        key |= static_cast<CacheKey>(std::get<0>(rowAbove));
        
        key <<= 2;
        key |= static_cast<CacheKey>(std::get<1>(rowAbove));
        
        key <<= 2;
        key |= static_cast<CacheKey>(std::get<2>(rowAbove));
        
        return key;
    }
    
    bool IsValidRow(const std::tuple<Color, Color, Color> row, const std::tuple<Color, Color, Color> rowAbove) const {
        return std::get<0>(row) != std::get<0>(rowAbove)
            && std::get<1>(row) != std::get<1>(rowAbove)
            && std::get<2>(row) != std::get<2>(rowAbove);
    }
    
    int Sum(int a, int b) const {
        auto sum = static_cast<std::uint64_t>(a);
        sum += b;
        return sum % MOD;
    }
    
    static constexpr auto ALL_POSSIBLE_ROWS = std::array<std::tuple<Color, Color, Color>, 12>{
        std::make_tuple(Color::GREEN, Color::RED, Color::GREEN),
        std::make_tuple(Color::GREEN, Color::RED, Color::YELLOW),
        std::make_tuple(Color::GREEN, Color::YELLOW, Color::GREEN),
        std::make_tuple(Color::GREEN, Color::YELLOW, Color::RED),
        
        std::make_tuple(Color::RED, Color::GREEN, Color::RED),
        std::make_tuple(Color::RED, Color::GREEN, Color::YELLOW),
        std::make_tuple(Color::RED, Color::YELLOW, Color::GREEN),
        std::make_tuple(Color::RED, Color::YELLOW, Color::RED),
        
        std::make_tuple(Color::YELLOW, Color::GREEN, Color::RED),
        std::make_tuple(Color::YELLOW, Color::GREEN, Color::YELLOW),
        std::make_tuple(Color::YELLOW, Color::RED, Color::GREEN),
        std::make_tuple(Color::YELLOW, Color::RED, Color::YELLOW),
    };
    
    static constexpr int MOD = 1000000007;
};


42155