题意:有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;
}