题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4739
思路:状态压缩。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<vector> 6 using namespace std; 7 8 struct Point{ 9 int x,y; 10 }point[22]; 11 12 int dp[(1<<20)+2]; 13 int n; 14 15 int cmp(const Point &p,const Point &q) 16 { 17 if(p.y==q.y){ 18 return p.x<q.x; 19 } 20 return p.y<q.y; 21 } 22 23 24 bool Judge(int i,int j,int k,int l) 25 { 26 if(point[i].x==point[j].x&&point[i].y==point[j].y)return false; 27 if(point[i].y!=point[j].y||point[i].x!=point[k].x||point[j].x!=point[l].x||point[k].y!=point[l].y) 28 return false; 29 if(abs(point[i].x-point[j].x)!=abs(point[i].y-point[k].y)) 30 return false; 31 return true; 32 } 33 34 vector<int>g[22]; 35 int main() 36 { 37 while(~scanf("%d",&n)){ 38 if(n==-1)break; 39 for(int i=0;i<n;i++)scanf("%d%d",&point[i].x,&point[i].y); 40 sort(point,point+n,cmp); 41 memset(dp,0,sizeof(dp)); 42 for(int i=0;i<=n;i++)g[i].clear(); 43 for(int i=0;i<n;i++){ 44 for(int j=i+1;j<n;j++){ 45 for(int k=j+1;k<n;k++){ 46 for(int l=k+1;l<n;l++){ 47 if(Judge(i,j,k,l)){ 48 int state=0; 49 state|=(1<<i); 50 state|=(1<<j); 51 state|=(1<<k); 52 state|=(1<<l); 53 g[i].push_back(state); 54 } 55 } 56 } 57 } 58 } 59 for(int state=0;state<(1<<n);state++){ 60 for(int i=0;i<n;i++){ 61 if(state&(1<<i)){ 62 for(int j=0;j<g[i].size();j++){ 63 int S=g[i][j]; 64 if((S|state)==state)dp[state]=max(dp[state],dp[state^S]+4); 65 } 66 } 67 } 68 } 69 printf("%d ",dp[(1<<n)-1]); 70 } 71 return 0; 72 }