HDU 4617 Weapon

题意:有n个圆柱,如果存在两个圆柱相交,就输出Lucky,否则输出在任意两圆柱距离的最小值

算一下直线间的距离就OK

#include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;

struct P
{
    double x, y, z;
    P(){}
    P(double _x, double _y, double _z){x = _x; y = _y; z = _z;}
};
P operator - (P a, P b) {return P(a.x-b.x, a.y-b.y, a.z-b.z);}
P cross(P a, P b) {return P(a.y*b.z - a.z*b.y, a.z*b.x - a.x*b.z, a.x*b.y - a.y*b.x);}
double dot(P a, P b) {return a.x*b.x + a.y*b.y + a.z*b.z;}
double dis(P a, P b) {return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z));}
double len(P a) {return sqrt(a.x*a.x + a.y*a.y + a.z*a.z);}

struct Weapon
{
    P p, s;
    double r;
}w[32];
int n;

double get_dis(P p1, P s1, P p2, P s2)
{
    P s = cross(s1, s2);
    if(len(s) < eps) return len(cross(s1, p2 - p1)) / len(s1);
    return fabs(dot(p2 - p1, s)) / len(s);
}

bool solve()
{
    double Min = 1e18;
    for(int i = 0; i < n; i++)
        for(int j = i + 1; j < n; j++)
        {
            double d = get_dis(w[i].p, w[i].s, w[j].p, w[j].s);
            if(d < w[i].r + w[j].r + eps) return true;
            Min = min(Min, d - w[i].r - w[j].r);
        }
    printf("%.2f\n", Min);
    return false;
}

int main()
{
    int T;
    scanf("%d", &T);

    while(T--)
    {
        scanf("%d", &n);
        for(int i = 0; i < n; i++)
        {
            P b, c;
            scanf("%lf%lf%lf", &w[i].p.x, &w[i].p.y, &w[i].p.z);
            P a = w[i].p;
            scanf("%lf%lf%lf", &b.x, &b.y, &b.z);
            scanf("%lf%lf%lf", &c.x, &c.y, &c.z);
            w[i].s = cross(b - a, c - a);
            w[i].r = dis(a, b);
        }

        if(solve()) printf("Lucky\n");
    }

    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值