Files
python/GESP/Level5/GESP202406/20240601.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

44 lines
1.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N][N]; // 原始数组
int s[N][N]; // 二维前缀和数组
int get(int x1, int y1, int x2, int y2) {
return s[x2][y2] - s[x1 - 1][y2] - s[x2][y1 - 1] + s[x1 - 1][y1 - 1];
}
/*
4 5 5
00000
01111
00011
00011
参考答案:6
*/
int main() {
int n, m, k;
cin >> n >> m >> k;
char c;
// 输入原始数组
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
cin >> c, a[i][j] = c - '0';
// 构建二维前缀和数组
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
s[i][j] = s[i - 1][j] + s[i][j - 1] - s[i - 1][j - 1] + a[i][j];
// 遍历每个可能的矩形,计算此矩形的二维前缀和,要求大于等k,求得最小的长乘以宽的值,也就是最小矩形大小
int ans = 1e9;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
for (int x = i; x <= n; x++)
for (int y = j; y <= m; y++)
if (get(i, j, x, y) >= k)
ans = min(ans, (x - i + 1) * (y - j + 1));
if(ans==1e9) ans=0;
cout << ans << endl;
return 0;
}