CF475C. Kamal-ol-molk's Painting
模拟即可,水题玩玩。代码能力略渣啊。。
#include <bits/stdc++.h> using namespace std; const int maxn = 1000 + 10; char g[maxn][maxn]; int n, m, minX, minY, ans; int sum[maxn][maxn], cnt, tot; int getSum(int a, int b, int x, int y) { return sum[a][b] - sum[a][y + 1] - sum[x + 1][b] + sum[x + 1][y + 1]; } bool ok(int a, int b, int posX = minX, int posY = minY) { while(1) { if(posX + a - 1 > n || posY + b - 1 > m) return false; if(getSum(posX, posY, posX + a - 1, posY + b - 1) != a * b) return false; if(getSum(posX, posY, n, m) == a * b && getSum(posX, posY, posX + a - 1, posY + b - 1) == a * b) { return (cnt + a * b == tot) ; } bool flag1 = (a * b) == getSum(posX + 1, posY, posX + a, posY + b - 1); bool flag2 = (a * b) == getSum(posX, posY + 1, posX + a - 1, posY + b); if(!flag1 && !flag2) return false; if(flag1 && flag2) return false; if(flag1) ++posX, cnt += b; else ++posY, cnt += a; } } int main() { scanf("%d%d", &n, &m); for(int i = 1; i <= n; ++i) { scanf("%s", g[i] + 1); } minX = n + 1, minY = m + 1; ans = n * m + 1; for(int i = n; 0 < i; --i) { for(int j = m; 0 < j; --j) { sum[i][j] = sum[i][j] - sum[i + 1][j + 1] + sum[i + 1][j] + sum[i][j + 1] + (g[i][j] == 'X'); if(g[i][j] == 'X') { minX = min(i, minX), minY = min(j, minY); ++tot; } } } for(int i = 1; i <= n; ++i) { for(int j = 1; j <= m; ++j) { if(ans <= i * j) continue; cnt = 0; if(ok(i, j)) ans = i * j; } } printf("%d\n", ans == n * m + 1 ? -1 : ans); return 0; }
人的一切痛苦,本质上都是对自己的无能的愤怒。