看到这题第一感觉就是dfs嘛, 毫无疑问,20个点最多也不过100个正方形(如果重复的话) 但题意不能重复,所以每次深搜都暴力出能组成正方形的所有情况就ok了!!
#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct node { int x,y; }num[30]; int cmp(node a,node b) { if(a.x!=b.x) return a.x<b.x; return a.y<b.y; } int abs(int a) { return a<0?-a:a; } int n; int mark[30],Max; int dfs(int cont) { int i; if(Max<cont) Max=cont; for(i=1;i<=n;i++) { if(mark[i]) continue; for(int j=1;j<=n;j++) { if(mark[j]) continue; if(num[i].x==num[j].x||num[i].y==num[j].y) continue; int x=abs(num[i].x-num[j].x); int y=abs(num[i].y-num[j].y); if(x!=y) continue; int x1=0,x2=0; for(int k=1;k<=n;k++) { if(mark[k]) continue; if(k==i||k==j) continue; if(num[k].x==num[j].x&&num[k].y==num[i].y) x1=k; if(num[k].x==num[i].x&&num[k].y==num[j].y) x2=k; } if(x1&&x2) { mark[i]=1; mark[j]=1; mark[x1]=1; mark[x2]=1; dfs(cont+1); mark[i]=0; mark[j]=0; mark[x1]=0; mark[x2]=0; } } } return 0; } int main() { int i,j; while(~scanf("%d",&n),n>0) { for(i=1;i<=n;i++) { scanf("%d%d",&num[i].x,&num[i].y); mark[i]=0; } sort(num+1,num+1+n,cmp); Max=0; dfs(0); printf("%d\n",Max*4); } return 0; }