您的位置:首页 > 其它

HDU 4617 Weapon

2015-11-13 01:04 387 查看
题意:有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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: