<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:
    bool isPrintable(vector<vector<int>>&v) {
        map<int,vector<int>>m;
        for(int i=0; i<v.size(); i++){
            for(int j=0; j<v[0].size(); j++){
                int curr=v[i][j];
                if(m[curr].size()==0) m[curr]={i,i,j,j};
                else {
                    m[curr][0]=min(m[curr][0],i);
                    m[curr][1]=max(m[curr][1],i);
                    m[curr][2]=min(m[curr][2],j);
                    m[curr][3]=max(m[curr][3],j);
                }
            }
        }
        map<int,vector<int>>adj;
        map<int,int>deg;
        for(auto [curr,vec]:m){
            int sti=vec[0],edi=vec[1],stj=vec[2],edj=vec[3];
            int ti=sti,tj=stj;
            for(; ti<=edi; ti++){
                for(; tj<=edj; tj++){
                    if(v[ti][tj]!=curr) {
                        adj[curr].push_back(v[ti][tj]);
                        deg[v[ti][tj]]++;
                    }
                }
                tj=stj;
            }
        }
        queue<int>q;
        vector<int>order;
        for(auto [val,_]:m){
            if(!deg[val]) q.push(val);
        }
        int ct=0;
        while(!q.empty()){
            int curr=q.front();
            order.push_back(curr);
            q.pop();
            for(auto i:adj[curr]){
                deg[i]--;
                if(!deg[i]) q.push(i);
            }
            ct++;
        }
        if(ct!=m.size()) return false;
        vector<vector<int>>ans(v.size(),vector<int>(v[0].size()));
        int i=0;
        while(i<order.size()){
            int curr=order[i];
            vector<int>vec=m[curr];
            int sti=vec[0],edi=vec[1],stj=vec[2],edj=vec[3];
            int ti=sti,tj=stj;
            for(; ti<=edi; ti++){
                for(; tj<=edj; tj++){
                    ans[ti][tj]=curr;
                }
                tj=stj;
            }
            i++;
        }
        return v==ans;
    }
};
48841