题目描述
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).
农民 John 的牛参加了一次和农民 Bob 的牛的竞赛。他们在区域中画了一个N*N 的正方形点阵,两个农场的牛各自占据了一些点。当然不能有两头牛处于同一个点。农场的目标是用自己的牛作为4个顶点,形成一个面积最大的正方形(不必须和边界平行) 。 除了 Bessie 以外,FJ其他的牛都已经放到点阵中去了,要确定bessie放在哪个位置,能使得农民约翰的农场得到一个最大的正方形(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
一开始很困惑样例是怎么来的,看了大神博客附的图就明白了
因为要加一个点,所以当前只需要得到能构成等腰直角三角形的三个点
数据规模100,可以枚举正方形对角线两点的横纵坐标,计算出另外两个点的坐标,当然要保证得出的四个点的坐标都在图内而且不与‘B’重合
每得到一个可行解就更新一遍答案
#include<bits/stdc++.h> using namespace std; const int N=100; string mat[N]; int main() { int n; cin>>n; for(int i=0;i<n;i++) cin>>mat[i]; int ans=0; for(int x1=0;x1<n;x1++) for(int y1=0;y1<n;y1++) { if(mat[x1][y1]=='B') continue; for(int x2=x1;x2<n;x2++) for(int y2=y1;y2<n;y2++) { if(mat[x2][y2]=='B'||(mat[x1][y1]=='.'&&mat[x2][y2]=='.')) continue; int dx=x2-x1,dy=y2-y1,x3=x1+dy,y3=y1-dx,x4=x2+dy,y4=y2-dx; if(x3>=0&&x3<n&&y3>=0&&y3<n&&x4>=0&&x4<n&&y4>=0&&y4<n&&mat[x3][y3]!='B'&&mat[x4][y4]!='B'&&(mat[x1][y1]=='J')+(mat[x2][y2]=='J')+(mat[x3][y3]=='J')+(mat[x4][y4]=='J')>=3) ans=max(ans,dx*dx+dy*dy); } } cout<<ans<<endl; return 0; }