洛谷 #2831. 愤怒的小鸟

题意

问最少用几条从原点出发的抛物线
即 \(ax^2+bx=0\) 能消灭全部的pig

题解

我是用dfs写的
对于每一个猪,检测是否已被击落,否则 暂时单独 or 与其他单独的猪组成抛物线

调试记录

有一个地方回溯忘记打了
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#define maxn 20
#define INF 0x3f3f3f3f

using namespace std;

int T, n, ans, tot = 0, totp = 0;
double tx[maxn], ty[maxn], px[maxn], py[maxn], x[maxn], y[maxn];

bool equ(double a, double b){
    if (fabs(a - b) < 1e-8) return true;
    return false;
}

void del(int pos){
    for (int j = pos; j < tot; j++) tx[j] = tx[j + 1], ty[j] = ty[j + 1];
    tot--;
}

void insert(int pos, double x, double y){
    for (int j = tot; j >= pos; j--) tx[j + 1] = tx[j], ty[j + 1] = ty[j];
    tot++;
    tx[pos] = x, ty[pos] = y;
}

bool pass(int dep){
    for (int k = 1; k <= totp; k++)
        if (equ(px[k] * x[dep] * x[dep] + py[k] * x[dep], y[dep])) return true;
    return false;
}

void dfs(int dep){
//	printf("%d %d\n", tot, totp); 
    if (tot + totp >= ans) return;
    if (dep == n + 1){
        ans = min(ans, tot + totp);
        return;
    }
    
    if (pass(dep)){
        dfs(dep + 1);
        return;
    }
    
    double x1 = x[dep], y1 = y[dep], x2 = 0, y2 = 0;
    for (int i = 1; i <= tot; i++){
        x2 = tx[i], y2 = ty[i];
        if (equ(x1, x2)) continue;
        double a = (x2 * y1 - x1 * y2) / (x1 * x1 * x2 - x1 * x2 * x2);
        double b = (x1 * x1 * y2 - x2 * x2 * y1) / (x1 * x2 * (x1 - x2));
        if (a < 0){
            px[++totp] = a, py[totp] = b;
            double ttx = tx[i], tty = ty[i];
            del(i);
            
            dfs(dep + 1);
            
            insert(i, ttx, tty);
            px[totp] = 0, py[totp--] = 0;
        }
    }
    
    tx[++tot] = x[dep], ty[tot] = y[dep];
    dfs(dep + 1);
    tx[tot] = 0, ty[tot--] = 0; 
}

int main(){
    scanf("%d", &T);
    
    while (T--){
        int opt;
        scanf("%d%d", &n, &opt);
        
        for (int i = 1; i <= n; i++) scanf("%lf%lf", &x[i], &y[i]);
        ans = INF;
        totp = 0; memset(px, 0, sizeof px), memset(py, 0, sizeof py);
        tot = 0; memset(tx, 0, sizeof tx), memset(ty, 0, sizeof ty);
        dfs(1);
        printf("%d\n", ans);
    }
    
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值