bzoj 1661: [Usaco2006 Nov]Big Square 巨大正方形 (枚举)

题目描述

传送门

题解

枚举矩形的两个端点(同一条边上的),然后求出剩下的两个端点,判断是否合法,并更新答案。

代码

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define N 103
using namespace std;
int cnt,n;
int mp[N][N];
struct data{
    int x,y;
}a[N*N];
int cmp(data a,data b)
{
    return a.x<b.x||a.x==b.x&&a.y<b.y;
}
int pow(int x)
{
    return x*x;
}
int main()
{
    scanf("%d",&n);
    for (int i=1;i<=n;i++){
        char s[N]; scanf("%s",s+1);
        for (int j=1;j<=n;j++) {
            if (s[j]=='B') mp[i][j]=-1;
            if (s[j]=='J') a[++cnt].x=i,a[cnt].y=j,mp[i][j]=1;
        }
    }
    int ans=0;
    for (int x1=1;x1<=n;x1++)
     for (int y1=1;y1<=n;y1++)
      for (int x2=x1+1;x2<=n;x2++)
       for (int y2=1;y2<=y1;y2++)
       {
         if (mp[x1][y1]==-1||mp[x2][y2]==-1) continue;
         int cnt=0;
         int len1=abs(x1-x2); int len2=abs(y1-y2);
         int x3=x1+len2; int y3=y1+len1;
         int x4=x2+len2; int y4=y2+len1;
         if (x3<1||y3<1||x3>n||y3>n) continue;
         if (x4<1||y4<1||x4>n||y4>n) continue;
         if (mp[x4][y4]==-1||mp[x3][y3]==-1) continue;
         cnt=mp[x1][y1]+mp[x2][y2]+mp[x3][y3]+mp[x4][y4];
         if (cnt<3) continue;
         ans=max(ans,pow(len1)+pow(len2));
      }
    printf("%d\n",ans);
}
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页