Bzoj 1661(枚举)

BZOJ 1661
Luogu 2867
from: USACO 2006 Nov Sliver(USACO刷题第7题)

枚举两个点,第二个点要在第一个点的右下,那么这样我们就可以根据一个公式来求另外两个点了。之后四个点求出来后判断四个点是否都可行,然后记录这四个点中J的数量,如果$>=3$,则这个正方形就是可行的,更新答案即可。

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
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;
const int MAXN = 100 + 5;
int n;
char s[MAXN][MAXN];
void clear() {
}
void init() {
clear();
for (int i=1;i<=n;i++) scanf("%s", s[i]+1);
}
void solve() {
int ans = 0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int x=i+1;x<=n;x++)
for (int y=1;y<=j;y++) {
int si = j - y, ti = x - i;
int x3 = i + si, y3 = j + ti, x4 = x + si, y4 = y + ti;
if (x3<=0||x3>n||x4<=0||x4>n||y3<=0||y3>n||y4<=0||y4>n) continue;
if (s[i][j]=='B'||s[x][y]=='B'||s[x3][y3]=='B'||s[x4][y4]=='B') continue;
int tot = 0;
if (s[i][j]=='J') tot++;
if (s[x][y]=='J') tot++;
if (s[x3][y3]=='J') tot++;
if (s[x4][y4]=='J') tot++;
if (tot>=3) ans = max(ans, si*si+ti*ti);
}
printf("%d\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d", &n)==1&&n) init(), solve();
return 0;
}

------ 本文结束 ------