NC24406. [USACO 2013 Ope G]Figure Eight
描述
输入描述
* Line 1: A single integer N, indicating the side length of the
marble.
* Lines 2..N+1: Each line describes a row of the marble, and contains
N characters which are each either '*' (an imperfection) or
'.' (a flawless section).
输出描述
* Line 1: The highest aesthetic score of any figure eight which
doesn't use any imperfect squares of the marble. If no figure
eight is attainable, then output -1.
示例1
输入:
15 ............... ............... ...*******..... .*....*.......* .*......*....*. ....*.......... ...*...****.... ............... ..**.*..*..*... ...*...**.*.... *..*...*....... ............... .....*..*...... .........*..... ...............
输出:
3888
C++11(clang++ 3.9) 解法, 执行用时: 152ms, 内存消耗: 2720K, 提交时间: 2020-07-08 12:59:28
#include<bits/stdc++.h> using namespace std; const int maxn=609; int n; char s[maxn][maxn]; int h[maxn]; int len[maxn][maxn]; int f[maxn][maxn]; int g[maxn][maxn]; long long ans; int main(){ scanf("%d",&n); for(int i=1;i<=n;++i)scanf("%s",s[i]+1); for(int i=1;i<=n;++i){ for(int j=1;j<=n;++j){ if(s[i][j]=='.')len[i][j]=len[i][j-1]+1; } } for(int i=1;i<=n;++i){ for(int j=i+1;j<=n;++j){ if(len[1][j]>=j-i+1)f[i][j]=1; } } for(int i=2;i<=n-1;++i){ for(int j=1;j<=n;++j){ for(int k=j+1;k<=n;++k){ if(!f[j][k]){ if(len[i][k]>=k-j+1)f[j][k]=1; }else{ if(s[i][j]=='.'&&s[i][k]=='.')f[j][k]++; else f[j][k]=0; } } } for(int j=n;j>=1;--j){ g[j][j]=0; for(int k=j+1;k<=n;++k){ g[j][k]=max(max(g[j][k-1],g[j+1][k]),(k-j-1)*(f[j][k]-2)); } } for(int j=1;j<=n;++j)h[j]=s[i][j]=='.'; for(int j=i+1;j<=n;++j){ for(int k=1;k<=n;++k)h[k]&=(s[j][k]=='.'); int l=1; for(int k=1;k<=n;++k){ if(!h[k])continue; while(!h[l]||l<=k-min(len[i][k],len[j][k]))++l; if(l==k)continue; ans=max(ans,1LL*(k-l-1)*(j-i-1)*g[l][k]); } } } if(ans)printf("%lld\n",ans); else printf("-1\n"); return 0; }