<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<int> overlap(vector<int> &a,vector<int> &b){
         int x1 = max(a[0],b[0]);
         int x2 = min(a[1],b[1]);
         int y1 = max(a[2],b[2]);
         int y2 = min(a[3],b[3]);

         ///cout<<x1<<","<<y1<<" "<<x2<<","<<y2<<endl;

         vector<int> tmp = {x1,x2,y1,y2};

         if(x1 > x2 || y1 > y2){
            return {};
         }else{
            return tmp;
         }
    }

    bool cycle = false;
    vector<int> vis;

    void dfs(int node,int par,vector<vector<int>> &g){
        vis[node] = 2;

        for(auto v:g[node]){
            if(!vis[v]){
                dfs(v,node,g);
            }else if(vis[v] == 2){
                cycle = true;
            }
        }

        vis[node] = 1;
    }

    bool isPrintable(vector<vector<int>>& arr) {
        int n = arr.size();
        int m = arr[0].size();

        vector<int> color;
        set<int> st;
        int mt = 0;
        for(int i=0;i<n;i++){
            for(int j=0;j<m;j++){
                if(st.find(arr[i][j]) == st.end()){
                    st.insert(arr[i][j]);
                    color.push_back(arr[i][j]);
                    mt = max(mt,arr[i][j]);
                }
            }
        }

        vector<vector<int>> rect(mt+1);
        for(auto v:color){
            int mx1=n,mx2=0,my1=m,my2=0;
            for(int i=0;i<n;i++){
                for(int j=0;j<m;j++){
                    if(arr[i][j] != v)continue;
                    mx1 = min(mx1,i);
                    mx2 = max(mx2,i);
                    my1 = min(my1,j);
                    my2 = max(my2,j);
                }
            }

            vector<int> tmp = {mx1,mx2,my1,my2};
            rect[v] = tmp;
        }

        // computing for the graph by checking for the overlapping region
        vector<vector<int>> g(mt+1);
        for(int i=0;i<color.size();i++){
            auto a = color[i];
            for(int j=i+1;j<color.size();j++){
                auto b = color[j];
                if(a==b)continue;

                auto aa = rect[a];
                auto bb = rect[b];

                auto it = overlap(aa,bb);
                if(it.size() == 0)continue;

                int color = -1;
                for(int i=it[0];i<=it[1];i++){
                    for(int j=it[2];j<=it[3];j++){
                        if(arr[i][j] == a){
                           if(color == -1){
                            color = a;
                           }else if(color == b){
                            return false;
                           }
                        }else if(arr[i][j] == b){
                            if(color == -1){
                                color = b;
                            }else if(color == a){
                                return false;
                            }
                        }
                    }
                }

                if(color == a){
                    g[b].push_back(a);
                }else if(color == b){
                    g[a].push_back(b);
                }

            }
        }

       

        for(auto v:color){
            vis.assign(mt+1,0);
            cycle = false;
            dfs(v,-1,g);
            if(cycle){
                return false;
            }
        }

        return true;
    }
};
21316