<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:
    vector<vector<int>> g;
    vector<vector<int>> adj;
    int n, m;
    void check_color(int color, int x_min, int x_max, int y_min, int y_max){
        set<int> curr_colors;
        for(int i = y_min; i <= y_max; i++){
            for(int j = x_min; j <= x_max; j++){
                curr_colors.insert(g[i][j]);
            }
        }
        for(int curr_color:curr_colors){
            if(curr_color == color) continue;
            adj[color].push_back(curr_color);
        }
    }
    bool check_dag(){
        vector<int> in_deg(adj.size());
        for(int i = 0; i < adj.size(); i++){
            for(int v:adj[i]) in_deg[v]++;
        }
        queue<int> q;
        for(int i = 0; i < adj.size(); i++){
            if(in_deg[i] == 0) q.push(i);
        }
        while(!q.empty()){
            int curr = q.front();
            q.pop();
            for(int v:adj[curr]){
                in_deg[v]--;
                if(in_deg[v] == 0) q.push(v);
            }
        }
        for(int i = 0; i < adj.size(); i++){
            if(in_deg[i]) return false;
        }
        return true;
    }
    bool isPrintable(vector<vector<int>>& targetGrid) {
        this->g = targetGrid;
        this->n = g.size();
        this->m = g[0].size();

        set<int> colors;
        for(int i = 0; i<n; i++){
            for(int j = 0; j<m; j++){
                colors.insert(g[i][j]);
            }
        }
        vector<vector<int>> adj(*colors.rbegin()+1);
        this->adj = adj;

        for(int color:colors){
            int x_min = m-1, x_max = 0, y_min = n-1, y_max = 0;
            for(int i = 0; i<n; i++){
                for(int j = 0; j<m; j++){
                    if(g[i][j] == color){
                        x_min = min(x_min, j);
                        x_max = max(x_max, j);
                        y_min = min(y_min, i);
                        y_max = max(y_max, i);
                    }
                }
            }
            check_color(color, x_min, x_max, y_min, y_max);
        }
        return check_dag();
    }
};
29115