[2018-2019 ACM-ICPC, Asia East Continent Finals] F. Interstellar … Fantasy (计算几何)

题目链接:F. Interstellar … Fantasy
题解

题目大意是在一个三维坐标内,给你两个点,两个点之间可能有一个球状的障碍物阻挡,你只能在球表面或者球外行走,问这两个点的最短距离。给的两个点不会出现在球内部。

本题的关键是最短路径的最优策略是如何走,本题可以转化成二维求解,圆心及两点能将障碍球切成一个大圆面,其实就是求两点不穿过圆在平面上的最短距离。
如果两点连线穿过圆面,答案=两点到圆短弧的切线+切点之间的圆弧距离
(为什么是切线呢?三角形两边之和大于第三边)

代码
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<unordered_set>
#include<unordered_map>
using namespace std;
//extern "C"{void *__dso_handle=0;}
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define pii pair<int,int>
#define lowbit(x) x&-x

const double PI=acos(-1.0);
const double eps=1e-6;
const ll mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

struct Point3
{
	double x,y,z;
	Point3(double _x=0,double _y=0,double _z=0)
	{
		x = _x;
		y = _y;
		z = _z;
	}
	void input()
	{
		scanf("%lf%lf%lf",&x,&y,&z);
	}
	double distance(const Point3 &b) const {
		return sqrt((x-b.x)*(x-b.x)+(y-b.y)*(y-b.y)+(z-b.z)*(z-b.z));
	}
	double len(){
		return sqrt(x*x+y*y+z*z);
	}
	Point3 operator /(const double &k) const{
		return Point3(x/k,y/k,z/k);
	}
	double operator *(const Point3 &b) const{
		return x*b.x+y*b.y+z*b.z;
	}
	Point3 operator ^(const Point3 &b) const{
		return Point3(y*b.z-z*b.y,z*b.x-x*b.z,x*b.y-y*b.x);
	}
	Point3 operator -(const Point3 &b) const{
		return Point3(x-b.x,y-b.y,z-b.z);
	}
};

int sgn(double x){
	if(fabs(x)<eps) return 0;
	if(x<0) return -1;
	else return 1;
}
struct Line3
{
	Point3 s,e;
	Line3() {}
	Line3(Point3 _s,Point3 _e){
		s = _s;
		e = _e;
	}
	double length() { return s.distance(e); }
	double dispointtoline(Point3 p)
	{
		return ((e-s)^(p-s)).len()/s.distance(e);
	}
	double dispointtoseg(Point3 p)
	{
		if(sgn((p-s)*(e-s))<0 || sgn((p-e)*(s-e))<0)
			return min(p.distance(s),e.distance(p));
		return dispointtoline(p);
	}
	bool Pointonseg(Point3 p)
	{
		return sgn( ((s-p)^(e-p)).len() )==0 && sgn((s-p)*(e-p))==0;
	}
};

int main()
{
	int t; scanf("%d",&t);
	while (t--) {
		Point3 o,s,t;
		double r;
		o.input(); scanf("%lf",&r);
		s.input(),t.input();
		Line3 l=Line3(s,t);
		double dis = l.dispointtoseg(o);
		if(sgn(dis-r)>=0)
		{
			printf("%.8lf\n",s.distance(t));
			continue;
		}
		double l1=s.distance(o),l2=t.distance(o),l3=s.distance(t);
		double ans=sqrt(l1*l1-r*r)+sqrt(l2*l2-r*r);
		Line3 st=Line3(s, t);
		double sum=acos((l1*l1+l2*l2-l3*l3)/(2.0*l1*l2));
		double sum1=acos(r/l1),sum2=acos(r/l2);
		ans+=r*(sum-sum1-sum2);
		printf("%.8lf\n",ans);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值