[USACO]Big Square 06NOV
题目描述
Farmer John's cows have entered into a competition with Farmer Bob's cows. They have drawn lines on the field so it is a square grid with N × N points (2 ≤ N ≤ 100), and each cow of the two herds has positioned herself exactly on a gridpoint. Of course, no two cows can stand in the same gridpoint. The goal of each herd is to form the largest square (not necessarily parallel to the gridlines) whose corners are formed by cows from that herd.
All the cows have been placed except for Farmer John's cow Bessie. Determine the area of the largest square that Farmer John's cows can form once Bessie is placed on the field (the largest square might not necessarily contain Bessie).
输入输出格式
输入格式
Line 1: A single integer, N
Lines 2..N+1: Line i+1 describes line i of the field with N characters. The characters are: 'J' for a Farmer John cow, 'B' for a Farmer Bob cow, and '*' for an unoccupied square. There will always be at least one unoccupied gridpoint.
输出格式
Line 1: The area of the largest square that Farmer John's cows can form, or 0 if they cannot form any square.
输入输出样例
输入样例
6
J*J***
******
J***J*
******
**B***
******
输出样例
4
提示
样例:
思路
枚举+语文题
枚举正方形对角线上的定点
时间复杂度:$$ O(n^4)$$
代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;
int n;
string Map[maxn];
int ans = 0;
int main()
{
scanf("%d", &n);
for (int i = 0; i < n; i++)
cin>>Map[i];
for (int x1 = 0; x1 < n; x1++)
for (int y1 = 0; y1 < n; y1++)
{
if (Map[x1][y1] == 'B') continue;
for (int x2 = x1; x2 < n; x2++)
for (int y2 = y1; y2 < n; y2++)
{
if (Map[x2][y2] == 'B' || (Map[x1][y1] == '.' && Map[x2][y2] == '.'))continue;
int a = x2 - x1, b = y2 - y1;
int x3 = x1 + b, y3 = y1 - a, x4 = x2 + b, y4 = y2 - a;
if (x3 >= 0 && x3 < n && y3 >= 0 && y3 < n && x4 >= 0 && x4 < n && y4 >= 0
&& y4 < n && (Map[x3][y3] != 'B') && (Map[x4][y4] != 'B') &&
((Map[x1][y1] == 'J') + (Map[x2][y2] == 'J') + (Map[x3][y3] == 'J') +
(Map[x4][y4] == 'J')) >= 3)
ans = max(ans, a*a + b*b);
}
}
printf("%d\n", ans);
}