预处理出只有四个1的情况存入数组中 然后状压下
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 using namespace std; 7 #define N 1050000 8 int dp[2][N]; 9 int q[2][N]; 10 int pt[N]; 11 bool f[N]; 12 struct node 13 { 14 int x,y; 15 }pp[25]; 16 bool cmp(node a,node b) 17 { 18 if(a.x==b.x) 19 return a.y<b.y; 20 return a.x<b.x; 21 } 22 int main() 23 { 24 int n,i,j,k,o,p[25][2]; 25 while(scanf("%d",&n)!=EOF) 26 { 27 if(n==-1) 28 break; 29 memset(dp,0,sizeof(dp)); 30 for(i = 0; i < n ; i++) 31 scanf("%d%d",&pp[i].x,&pp[i].y); 32 sort(pp,pp+n,cmp); 33 k = 0; 34 for(i = 0 ; i < 1<<n ; i++) 35 { 36 o = 0; 37 for(j = 0 ; j < n ; j++) 38 { 39 if(i&(1<<j)) 40 { 41 o++; 42 p[o][0] = pp[j].x; 43 p[o][1] = pp[j].y; 44 } 45 } 46 if(o==4) 47 { 48 int x1,x2; 49 if(p[1][0]==p[2][0]&&p[3][0]==p[4][0]&&p[2][1]==p[4][1]&&p[1][1]==p[3][1]) 50 { 51 x1 = p[2][1]-p[1][1]; 52 x2 = p[3][0]-p[1][0]; 53 if(x1==x2) 54 { 55 k++; 56 q[1][k] = i; 57 dp[1][i] = 4; 58 pt[k] = i; 59 } 60 } 61 } 62 } 63 int tt = k; 64 o=k; 65 for(i = 2; i <= n/4+1 ; i++) 66 { 67 k = tt;tt=0; 68 for(j = 1 ; j <= k ; j++) 69 { 70 for(int g = 1 ; g <= o ; g++) 71 { 72 if(q[(i-1)%2][j]&pt[g]) 73 continue; 74 int x = q[(i-1)%2][j]+pt[g]; 75 dp[i%2][x] = max(dp[i%2][x],dp[(i-1)%2][q[(i-1)%2][j]]+4); 76 if(!f[x]) 77 { 78 f[x] = 1; 79 tt++; 80 q[i%2][tt] = x; 81 } 82 } 83 } 84 } 85 int ans=0; 86 for(i = 0 ; i < 1<<n ; i++) 87 { 88 ans = max(ans,max(dp[0][i],dp[1][i])); 89 } 90 printf("%d\n",ans); 91 } 92 return 0; 93 }