NC25086. [USACO 2006 Nov S]Big Square
描述
输入描述
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.
示例1
输入:
6 J*J*** ****** J***J* ****** **B*** ******
输出:
4
C++11(clang++ 3.9) 解法, 执行用时: 157ms, 内存消耗: 476K, 提交时间: 2019-07-04 19:59:00
#include<bits/stdc++.h> using namespace std; char t[110][110]; int main(){ int n,ans=0; cin>>n; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) cin>>t[i][j]; for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) for(int i1=i+1;i1<=n;i1++) for(int j1=1;j1<=j;j1++){ int i4=j-j1,j4=i1-i; int i2=i+i4,j2=j+j4,i3=i1+i4,j3=j1+j4; if(i2<1||i2>n||j2<1||j2>n||i3<1||i3>n||j3<1||j3>n||t[i][j]=='B'||t[i1][j1]=='B'||t[i2][j2]=='B'||t[i3][j3]=='B')continue; int s=0; if(t[i][j]=='J')s++;if(t[i1][j1]=='J')s++;if(t[i2][j2]=='J')s++;if(t[i3][j3]=='J')s++; if(s>=3)ans=max(ans,i4*i4+j4*j4); } cout<<ans<<"\n"; return 0; }