lightoj1018 (compression state dp)

Today, more than an hour to write, and only then shaped pressure dp written. (Or done under the guidance of the DY)
However, it also summarizes some knowledge:
1. For previous difference of 0 to n-1 and 1 ~ n is not clear, the original just use the last one, but the memory a large gap.
2. For understanding-bit computing, but also to the next level, it is good.
3. Pressure dp-like basis and understanding has deepened.
4. For the pre-shaped pressure is represented with the binary
5. The pressure dp like can also be used to resolve dfs

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define ak(aa) memset(aa,0,sizeof(aa))
#define For(aa,bb,cc) for(int aa=bb;aa<=cc;++aa)
using namespace std;
const int maxn=1<<18,inf=0x3f3f3f3f;
int dp[maxn];
int line[20][20];
int n;
struct node{
    int x,y;
}a[20];
/*
dp[]中用0表示已经被抹除,1表示还未被抹除
line[i][j]用1表示这个点在i,j之间的连线上,0表示不在
*/

void prepare(){
    For(i,1,n){
        For(j,i+1,n){
            int lx=a[i].x-a[j].x,ly=a[i].y-a[j].y;
            line[i][j]=(1<<i)|(1<<j);
            For(k,j+1,n){
                int mx=a[i].x-a[k].x,my=a[i].y-a[k].y;
                if(lx*my==mx*ly){
                    line[i][j]|=(1<<k);
                }
            }
            line[j][i]=line[i][j];
        }
    }
    return ;
}

int dfs(int now){
    if(dp[now]<inf) return dp[now];
    int num=__builtin_popcount(now);//查找1 的个数
    if(num<=2) return 1;
    int i=1;
    while(!(now&(1<<i))) ++i;//找到第一个存在的点
    For(j,i+1,n){//进行枚举点
        if(now&(1<<j))  
            dp[now]=min(dp[now],dfs(now&(~line[i][j]))+1);
    }
    return dp[now];
}

void work(int now){
    memset(dp,inf,sizeof(dp));
    dp[0]=0;//初值
    scanf("%d",&n);
    For(i,1,n){
        scanf("%d%d",&a[i].x,&a[i].y);
    }
    prepare();
    printf("Case %d: ",now);
    printf("%d\n",dfs((1<<n+1)-2));//全集要设定好
    return ;
}

int main(){
    int _;
    scanf("%d",&_);
    For(i,1,_) work(i);
    return 0;
}
Published 51 original articles · won praise 6 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_35776579/article/details/54382136