hde 4739 Zhuge Liang's Mines(状态压缩DP,4级)

来源:互联网 发布:跪求十宗罪网络剧种子 编辑:程序博客网 时间:2024/04/29 07:03

Zhuge Liang's Mines

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 306    Accepted Submission(s): 139


Problem Description
In the ancient three kingdom period, Zhuge Liang was the most famous and smartest military leader. His enemy was Shima Yi, who always looked stupid when fighting against Zhuge Liang. But it was Shima Yi who laughed to the end.

Once, Zhuge Liang sent the arrogant Ma Shu to defend Jie Ting, a very important fortress. Because Ma Shu is the son of Zhuge Liang's good friend Ma liang, even Liu Bei, the Ex. king, had warned Zhuge Liang that Ma Shu was always bragging and couldn't be used, Zhuge Liang wouldn't listen. Shima Yi defeated Ma Shu and took Jie Ting. Zhuge Liang had to kill Ma Shu and retreated. To avoid Shima Yi's chasing, Zhuge Liang put some mines on the only road. Zhuge Liang deployed the mines in a Bagua pattern which made the mines very hard to remove. If you try to remove a single mine, no matter what you do ,it will explode. Ma Shu's son betrayed Zhuge Liang , he found Shima Yi, and told Shima Yi the only way to remove the mines: If you remove four mines which form the four vertexes of a square at the same time, the removal will be success. In fact, Shima Yi was not stupid. He removed as many mines as possible. Can you figure out how many mines he removed at that time?

The mine field can be considered as a the Cartesian coordinate system. Every mine had its coordinates. To simplify the problem, please only consider the squares which are parallel to the coordinate axes.
 

Input
There are no more than 15 test cases.
In each test case:

The first line is an integer N, meaning that there are N mines( 0 < N <= 20 ).

Next N lines describes the coordinates of N mines. Each line contains two integers X and Y, meaning that there is a mine at position (X,Y). ( 0 <= X,Y <= 100)

The input ends with N = -1.
 

Output
For each test case ,print the maximum number of mines Shima Yi removed in a line.
 

Sample Input
31 10 02 280 01 02 00 11 12 110 110 0-1
 

Sample Output
04
 

Source
2013 ACM/ICPC Asia Regional Hangzhou Online
 

Recommend
liuyiding
 
思路:两个对角两个点可以确定一个正方形,所以用状态压缩DP转移一次得 n^2  ,最多构成n^2个不同矩形。

#include<cstring>#include<cstdio>#include<iostream>#define FOR(i,a,b) for(int i=a;i<=b;++i)#define clr(f,z) memset(f,z,sizeof(f))#define ll(x) (1<<x)using namespace std;const int mm=22;class PPP{  public:int x,y;}p[mm];class SQRE{public:  int a,b,c,d;}sq[mm*mm*mm];bool dp[1<<22];int n,pos;bool ok(int a,int b,int c,int d){ if(a==b||a==c||a==d||b==c||b==d||c==d)return 0;  if(p[a].x>p[b].x)swap(a,b);  if(p[a].x>p[c].x)swap(a,c);  if(p[a].x>p[d].x)swap(a,d);  if(p[b].x>p[c].x)swap(b,c);  if(p[b].x>p[d].x)swap(b,d);  if(p[a].x!=p[b].x)return 0;  if(p[c].x!=p[d].x)return 0;  if(p[a].x==p[c].x)return 0;  if(p[a].y>p[b].y)swap(a,b);  if(p[c].y>p[d].y)swap(c,d);  if(p[a].y!=p[c].y)return 0;  if(p[b].y!=p[d].y)return 0;  if(p[a].y==p[b].y)return 0;  int aa=p[c].x-p[a].x;  int bb=p[b].y-p[a].y;  if(aa!=bb)return 0;  return 1;}void DP(int x){ int z=0;  dp[x]=1;  for(int i=0;i<pos;++i)  {    z=ll(sq[i].a)|ll(sq[i].b)|ll(sq[i].c)|ll(sq[i].d);    if(z&x)continue;   // printf("%d - %d\n",z,x);   // int zz=(z&x);    //cout<<zz<<endl;    DP(x|z);  }}int get(int x){  int ret=0;  while(x)  {    ret+=(x%2);x/=2;  }  return ret;}int main(){  while(~scanf("%d",&n))  { if(n==-1)break;    FOR(i,0,n-1)    scanf("%d%d",&p[i].x,&p[i].y);    pos=0;    FOR(i,0,n-1)    FOR(j,i+1,n-1)    FOR(k,j+1,n-1)    FOR(l,k+1,n-1)    if(ok(i,j,k,l))      sq[pos].a=i,sq[pos].b=j,sq[pos].c=k,sq[pos++].d=l;    clr(dp,0);    dp[0]=1;    DP(0);    int z=ll(n),ans=0;    for(int i=0;i<z;++i)      if(dp[i])      {ans=max(ans,get(i));       //cout<<i<<" "<<get(i)<<endl;      }    printf("%d\n",ans);  }}