-
Notifications
You must be signed in to change notification settings - Fork 2
/
stamping_the_grid.cpp
79 lines (70 loc) · 2.16 KB
/
stamping_the_grid.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// 用邮票贴满网格图
// https://leetcode.cn/problems/stamping-the-grid
// INLINE ../../images/array/stamping_the_grid.jpeg
#include <headers.hpp>
class Solution {
public:
bool possibleToStamp(vector<vector<int>> &grid, int stampHeight,
int stampWidth) {
int m = grid.size(), n = grid[0].size();
// 检查网格是否全由1组成
bool allOnes = true;
for (const auto &row : grid) {
for (int cell : row) {
if (cell == 0) {
allOnes = false;
break;
}
}
if (!allOnes)
break;
}
if (allOnes)
return true;
// 如果邮票尺寸大于网格本身,无法放置邮票
if (stampHeight > m || stampWidth > n) {
return false;
}
vector<vector<int>> prefixSum(m + 1, vector<int>(n + 1, 0));
vector<vector<int>> diff(m + 1, vector<int>(n + 1, 0));
// 计算前缀和
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
prefixSum[i][j] = grid[i - 1][j - 1] + prefixSum[i - 1][j] +
prefixSum[i][j - 1] - prefixSum[i - 1][j - 1];
}
}
// 使用差分数组记录邮票放置情况
for (int i = 1; i <= m - stampHeight + 1; ++i) {
for (int j = 1; j <= n - stampWidth + 1; ++j) {
int x = i + stampHeight - 1, y = j + stampWidth - 1;
if (prefixSum[x][y] - prefixSum[x][j - 1] - prefixSum[i - 1][y] +
prefixSum[i - 1][j - 1] ==
0) {
diff[i][j]++;
if (y + 1 <= n)
diff[i][y + 1]--;
if (x + 1 <= m)
diff[x + 1][j]--;
if (x + 1 <= m && y + 1 <= n)
diff[x + 1][y + 1]++;
}
}
}
// 更新差分数组并检查是否每个空白格都至少被一个邮票覆盖
for (int i = 1; i <= m; ++i) {
for (int j = 1; j <= n; ++j) {
if (i > 1)
diff[i][j] += diff[i - 1][j];
if (j > 1)
diff[i][j] += diff[i][j - 1];
if (i > 1 && j > 1)
diff[i][j] -= diff[i - 1][j - 1];
if (diff[i][j] == 0 && grid[i - 1][j - 1] == 0) {
return false;
}
}
}
return true;
}
};