<p>There is a strange printer with the following two special requirements:</p>

<ul>
	<li>On each turn, the printer will print a solid rectangular pattern of a single color on the grid. This will cover up the existing colors in the rectangle.</li>
	<li>Once the printer has used a color for the above operation, <strong>the same color cannot be used again</strong>.</li>
</ul>

<p>You are given a <code>m x n</code> matrix <code>targetGrid</code>, where <code>targetGrid[row][col]</code> is the color in the position <code>(row, col)</code> of the grid.</p>

<p>Return <code>true</code><em> if it is possible to print the matrix </em><code>targetGrid</code><em>,</em><em> otherwise, return </em><code>false</code>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print1.jpg" style="width: 600px; height: 175px;" />
<pre>
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,2,2,1],[1,2,2,1],[1,1,1,1]]
<strong>Output:</strong> true
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/12/23/print2.jpg" style="width: 600px; height: 367px;" />
<pre>
<strong>Input:</strong> targetGrid = [[1,1,1,1],[1,1,3,3],[1,1,3,4],[5,5,1,4]]
<strong>Output:</strong> true
</pre>

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

<pre>
<strong>Input:</strong> targetGrid = [[1,2,1],[2,1,2],[1,2,1]]
<strong>Output:</strong> false
<strong>Explanation:</strong> It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
</pre>

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

<ul>
	<li><code>m == targetGrid.length</code></li>
	<li><code>n == targetGrid[i].length</code></li>
	<li><code>1 &lt;= m, n &lt;= 60</code></li>
	<li><code>1 &lt;= targetGrid[row][col] &lt;= 60</code></li>
</ul>

class Solution {
public:
    typedef struct Rectangle
    {
        int min_i;
        int max_i;
        int min_j;
        int max_j;
    }Rectangle;

    Rectangle getGeneratorRectangle(vector<pair<int, int>>& points, int m, int n)
    {
        int min_i = m;
        int max_i = -1;
        int min_j = n;
        int max_j = -1;

        for(pair<int, int>& point : points)
        {
            min_i = min(point.first, min_i);
            max_i = max(point.first, max_i);
            min_j = min(point.second, min_j);
            max_j = max(point.second, max_j);
        }

        return {
            min_i = min_i,
            max_i = max_i,
            min_j = min_j,
            max_j = max_j
        };
    }

    // -1 if incompatible
    // 0 if is compatible and order doesn't matter
    // 1 if is compatible and first rectangle goes first
    // 2 if is compatible and second rectangle goes first
    int compareCompatibility(Rectangle&& r1, int color1, Rectangle&& r2, int color2, const vector<vector<int>>& grid)
    {
        if(r2.min_i > r1.max_i || r1.min_i > r2.max_i || r2.min_j > r1.max_j || r1.min_j > r2.max_j)
            return 0;

        int min_i = max(r1.min_i, r2.min_i);
        int max_i = min(r1.max_i, r2.max_i);
        int min_j = max(r1.min_j, r2.min_j);
        int max_j = min(r1.max_j, r2.max_j);

        int expectedColor = -1;
        for(int i=min_i; i<=max_i; ++i)
        {
            for(int j=min_j; j<=max_j; ++j)
            {
                if(grid[i][j] != color1 && grid[i][j] != color2)
                    continue;
                if(expectedColor == -1)
                {
                    expectedColor = grid[i][j];
                }
                else if(grid[i][j] != expectedColor)
                {
                    return -1;
                }
            }
        }

        if(expectedColor == -1)
            return 0;
        if(expectedColor == color1)
            return 2;
        return 1;
    }

    bool isCyclic(int color, unordered_map<int, vector<int>>& dependsOn, unordered_map<int, bool> &visited, unordered_map<int, bool> &computed)
    {
        if(visited[color])
            return true;
        visited[color] = true;

        bool cyclic = false;
        for(int dependencyStep : dependsOn[color])
        {
            if(!computed[dependencyStep] && isCyclic(dependencyStep, dependsOn, visited, computed))
            {
                cyclic = true;
                break;
            }
        }

        visited[color] = false;
        computed[color] = true;

        return cyclic;
    }

    bool isPrintable(vector<vector<int>>& grid)
    {
        int m = grid.size();
        int n = grid[0].size();
        unordered_map<int, vector<pair<int, int>>> colourPoints;
        unordered_map<int, vector<int>> dependsOn;

        for (int i=0; i<m; ++i)
        {
            for (int j=0; j<n; ++j)
            {
                colourPoints[grid[i][j]].push_back(make_pair(i, j));
            }
        }

        
        for(auto it = colourPoints.begin(); it != colourPoints.end(); ++it)
        {
            int color1 = it->first;
            vector<pair<int, int>>& points1 = it->second;
            for(auto it2 = next(it, 1); it2 != colourPoints.end(); ++it2)
            {
                int color2 = it2 -> first;
                vector<pair<int, int>>& points2 = it2 -> second;

                int compatibilityCompare = compareCompatibility(getGeneratorRectangle(points1, m, n), color1, getGeneratorRectangle(points2, m, n), color2, grid);

                if(compatibilityCompare == -1)
                    return false;
                else if(compatibilityCompare == 1)
                    dependsOn[color2].push_back(color1);
                else if(compatibilityCompare == 2)
                    dependsOn[color1].push_back(color2);
            }
        }

        unordered_map<int, bool> visited;
        unordered_map<int, bool> computed;
        for(auto it = dependsOn.begin(); it != dependsOn.end(); ++it)
        {
            if(!computed[it->first])
                if(isCyclic(it->first, dependsOn, visited, computed))
                    return false;
        }

        return true;
    }
};
22693