其他分享
首页 > 其他分享> > luogu_2831: 愤怒的小鸟

luogu_2831: 愤怒的小鸟

作者:互联网

洛谷2831:愤怒的小鸟(状压\(dp\))

题意:

输入格式:

输出格式:

思路:

代码:

#include<bits/stdc++.h>
using namespace std;
const int maxn = 25, INF = 0x3f3f3f3f;

int T, n, m;
int line[maxn][maxn];
int f[1<<maxn];
double x[maxn], y[maxn];

inline bool same(double x, double y){
    return fabs(x - y) < 1e-6;
}

void init()
{
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++)
        scanf("%lf%lf", &x[i], &y[i]);
    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            line[i][j] = 0;
    
    //考虑ij每一种组合
    for(int i = 1; i <= n-1; i++)
        for(int j = i + 1; j <= n; j++)
        {
            if(same(x[i], x[j])) continue;  //如果他们在同一列
            double aa = (y[j]/x[j] - y[i]/x[i]) / (x[j]-x[i]);
            if(aa >= 0) continue;
            double bb = y[i]/x[i] - aa*x[i];
            for(int k = 1; k <= n; k++)
                if(same(aa*x[k]+bb, y[k]/x[k])) //如果他在这条抛物线上
                    line[i][j] |= (1<<(k-1));
        }
    for(int i = 0; i <= (1<<n)-1; i++) f[i] = INF; f[0] = 0;
}

int main()
{
    scanf("%d", &T);
    while(T--)
    {
        init();
        for(int i = 0; i <= (1<<n)-1; i++)
        {
            for(int j = 1; j <= n; j++)
                if(!(i&(1<<(j-1)))) //如果这里有一个没有穿过的点
                {
                    for(int k = j; k <= n; k++) //前面的已经串好了
                    {
                        if(j == k) 
                            f[i|(1<<(j-1))] = min(f[i|(1<<(j-1))], f[i]+1);
                        if(same(x[j], x[k])) continue;
                        //用已有的线好还是再多一条线好
                        f[i|line[j][k]] = min(f[i|line[j][k]], f[i]+1);
                    }
                    break;
                }
        }
        //输出答案
        printf("%d\n", f[(1<<n)-1]);
    }
    return 0;
}

标签:int,luogu,小鸟,抛物线,输入,穿过,maxn,line,2831
来源: https://www.cnblogs.com/zxytxdy/p/11644957.html