USACO Home on the Range

Code

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
/*
ID: wcr19961
PROG: range
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long LL;
const int maxn = 255;
char s[maxn][maxn];
int sum[maxn][maxn], ans[maxn];
int Sum(int x, int y, int a, int b) {
return sum[a][b] - sum[x - 1][b] - sum[a][y - 1] + sum[x - 1][y - 1];
}
int main() {
freopen("range.in", "r", stdin);
freopen("range.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= n; ++j) {
sum[i][j] = s[i][j] == '1' ? 1 : 0;
sum[i][j] += sum[i][j - 1];
}
for (int j = 1; j <= n; ++j) {
sum[i][j] += sum[i - 1][j];
}
}
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
for (int k = 2; k <= n; ++k) {
if (i + k <= n + 1 && j + k <= n + 1) {
if (Sum(i, j, i + k - 1, j + k - 1) == k * k) {
++ans[k];
}
}
}
}
}
for (int i = 2; i <= n; ++i) {
if (ans[i] > 0) {
printf("%d %d\n", i, ans[i]);
}
}
return 0;
}

支付宝扫码领红包