There is a strange printer with the following two special requirements:
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.
Once the printer has used a color for the above operation, the same color cannot be used again.
You are given a m x n matrix targetGrid, where targetGrid[row][col] is the color in the position (row, col) of the grid.
Return trueif it is possible to print the matrixtargetGrid
_,_otherwise, returnfalse.
Input: targetGrid =[[1,2,1],[2,1,2],[1,2,1]]Output: falseExplanation: It is impossible to form targetGrid because it is not allowed to print the same color in different turns.
For each color, find its minimal bounding rectangle. If any other color appears inside this rectangle, that color must be printed before the current color. This forms a dependency graph. If the dependency graph is acyclic (can be topologically sorted), the answer is true; otherwise, false.
#include<vector>#include<queue>usingnamespace std;
boolisPrintable(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<int> minr(61, m), maxr(61, -1), minc(61, n), maxc(61, -1);
for (int i =0; i < m; ++i) for (int j =0; j < n; ++j) {
int c = grid[i][j];
minr[c] = min(minr[c], i); maxr[c] = max(maxr[c], i);
minc[c] = min(minc[c], j); maxc[c] = max(maxc[c], j);
}
vector<vector<int>> g(61);
vector<int> indeg(61);
for (int c =1; c <=60; ++c) if (maxr[c] !=-1) {
for (int i = minr[c]; i <= maxr[c]; ++i)
for (int j = minc[c]; j <= maxc[c]; ++j)
if (grid[i][j] != c) {
g[c].push_back(grid[i][j]);
indeg[grid[i][j]]++;
}
}
queue<int> q;
for (int c =1; c <=60; ++c) if (maxr[c] !=-1&& indeg[c] ==0) q.push(c);
int cnt =0;
while (!q.empty()) {
int c = q.front(); q.pop(); cnt++;
for (int nei : g[c]) if (--indeg[nei] ==0) q.push(nei);
}
int total =0;
for (int c =1; c <=60; ++c) if (maxr[c] !=-1) total++;
return cnt == total;
}
import java.util.*;
classSolution {
publicbooleanisPrintable(int[][] grid) {
int m = grid.length, n = grid[0].length;
int[] minr =newint[61], maxr =newint[61], minc =newint[61], maxc =newint[61];
Arrays.fill(minr, m); Arrays.fill(maxr, -1); Arrays.fill(minc, n); Arrays.fill(maxc, -1);
for (int i = 0; i < m; i++) for (int j = 0; j < n; j++) {
int c = grid[i][j];
minr[c]= Math.min(minr[c], i); maxr[c]= Math.max(maxr[c], i);
minc[c]= Math.min(minc[c], j); maxc[c]= Math.max(maxc[c], j);
}
List<Integer>[] g =new List[61];
for (int i = 0; i < 61; i++) g[i]=new ArrayList<>();
int[] indeg =newint[61];
for (int c = 1; c <= 60; c++) if (maxr[c]!=-1) {
for (int i = minr[c]; i <= maxr[c]; i++)
for (int j = minc[c]; j <= maxc[c]; j++)
if (grid[i][j]!= c) {
g[c].add(grid[i][j]);
indeg[grid[i][j]]++;
}
}
Queue<Integer> q =new ArrayDeque<>();
for (int c = 1; c <= 60; c++) if (maxr[c]!=-1 && indeg[c]== 0) q.add(c);
int cnt = 0, total = 0;
for (int c = 1; c <= 60; c++) if (maxr[c]!=-1) total++;
while (!q.isEmpty()) {
int c = q.poll(); cnt++;
for (int nei : g[c]) if (--indeg[nei]== 0) q.add(nei);
}
return cnt == total;
}
}
funisPrintable(grid: Array<IntArray>): Boolean {
val m = grid.size; val n = grid[0].size
val minr = IntArray(61) { m }; val maxr = IntArray(61) { -1 }
val minc = IntArray(61) { n }; val maxc = IntArray(61) { -1 }
for (i in0 until m) for (j in0 until n) {
val c = grid[i][j]
minr[c] = minOf(minr[c], i); maxr[c] = maxOf(maxr[c], i)
minc[c] = minOf(minc[c], j); maxc[c] = maxOf(maxc[c], j)
}
val g = Array(61) { mutableListOf<Int>() }
val indeg = IntArray(61)
for (c in1..60) if (maxr[c] != -1) {
for (i in minr[c]..maxr[c]) for (j in minc[c]..maxc[c])
if (grid[i][j] != c) {
g[c].add(grid[i][j])
indeg[grid[i][j]]++ }
}
val q = ArrayDeque<Int>()
for (c in1..60) if (maxr[c] != -1&& indeg[c] ==0) q.add(c)
var cnt = 0; var total = 0for (c in1..60) if (maxr[c] != -1) total++while (q.isNotEmpty()) {
val c = q.removeFirst(); cnt++for (nei in g[c]) if (--indeg[nei] ==0) q.add(nei)
}
return cnt == total
}
defisPrintable(targetGrid: list[list[int]]) -> bool:
m, n = len(targetGrid), len(targetGrid[0])
minr = [m]*61; maxr = [-1]*61; minc = [n]*61; maxc = [-1]*61for i in range(m):
for j in range(n):
c = targetGrid[i][j]
minr[c] = min(minr[c], i); maxr[c] = max(maxr[c], i)
minc[c] = min(minc[c], j); maxc[c] = max(maxc[c], j)
g = [[] for _ in range(61)]
indeg = [0]*61for c in range(1, 61):
if maxr[c] ==-1: continuefor i in range(minr[c], maxr[c]+1):
for j in range(minc[c], maxc[c]+1):
if targetGrid[i][j] != c:
g[c].append(targetGrid[i][j])
indeg[targetGrid[i][j]] +=1 q = [c for c in range(1, 61) if maxr[c] !=-1and indeg[c] ==0]
cnt =0; total = sum(1for c in range(1, 61) if maxr[c] !=-1)
while q:
c = q.pop(0); cnt +=1for nei in g[c]:
indeg[nei] -=1if indeg[nei] ==0:
q.append(nei)
return cnt == total