<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:
    // Depth-First Search (DFS) function to detect cycles in the graph
    bool dfs(int node, vector<int>& vis, vector<int>& topo, vector<vector<int>>& graph) {
        vis[node] = 1; // Mark current node as visited (in process)
        
        // Traverse all nodes connected to the current node
        for (auto it : graph[node]) {
            // If the connected node is already being visited, cycle detected
            if (vis[it] == 1) return false;
            
            // If the connected node is not visited, recursively call DFS on it
            if (vis[it] == 0 && !dfs(it, vis, topo, graph)) return false;
        }
        
        vis[node] = 2; // Mark current node as visited (processed)
        topo.push_back(node); // Push current node to topological sort order
        return true;
    }

    bool isPrintable(vector<vector<int>>& grid) {
        int n = grid.size();
        int m = grid[0].size();
        
        // Initialize graph with 61 nodes (colors range from 1 to 60)
        vector<vector<int>> graph(61);
        
        // Array to store bounds of each color: left, right, top, bottom
        int color[61][4]; // 61 because colors range from 1 to 60
        
        // Initialize color bounds to -1 (indicating unused colors)
        memset(color, -1, sizeof(color));

        // Step 1: Determine the bounds of each color in the grid
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < m; ++j) {
                int x = grid[i][j];
                if (color[x][0] == -1) {
                    color[x][0] = j;  // left
                    color[x][1] = j;  // right
                    color[x][2] = i;  // top
                    color[x][3] = i;  // bottom
                } else {
                    color[x][0] = min(color[x][0], j);  // left
                    color[x][1] = max(color[x][1], j);  // right
                    color[x][2] = min(color[x][2], i);  // top
                    color[x][3] = max(color[x][3], i);  // bottom
                }
            }
        }

        // Step 2: Construct the graph based on overlapping conditions
        for (int i = 1; i <= 60; ++i) {
            if (color[i][0] == -1) continue; // Skip unused colors
            
            for (int j = 1; j <= 60; ++j) {
                if (i == j || color[j][0] == -1) continue; // Skip same color or unused color
                
                bool overlap = false;
                
                // Check if there is an overlap between color i and color j
                for (int r = color[i][2]; r <= color[i][3]; ++r) {
                    for (int c = color[i][0]; c <= color[i][1]; ++c) {
                        if (grid[r][c] == j) {
                            overlap = true;
                            break;
                        }
                    }
                    if (overlap) break;
                }
                
                // If there is an overlap, add an edge from i to j in the graph
                if (overlap) {
                    graph[i].push_back(j);
                }
            }
        }

        // Step 3: Check for cycles in the graph using DFS
        vector<int> topo; // Stores nodes in topological sort order
        vector<int> vis(61, 0); // Array to track visit status of nodes
        
        for (int i = 1; i <= 60; ++i) {
            if (color[i][0] != -1 && vis[i] == 0) {
                // If the current color is not visited, perform DFS
                if (!dfs(i, vis, topo, graph)) {
                    return false; // Cycle detected, print order not possible
                }
            }
        }

        for (int i = 1; i <= 60; ++i) {
            if (!graph[i].empty()) {
                cout << i << ": ";
                for (int j = 0; j < graph[i].size(); ++j) {
                    cout << graph[i][j] << " ";
                }
                cout << endl;
            }
        }
        
        // Print topological sort
        for (int i = 0; i < topo.size(); i++) {
            cout << topo[i] << " ";
        }

        return true; // No cycles detected, print order is possible
    }
};

19481