1. 程式人生 > 其它 >202009-4 星際旅行

202009-4 星際旅行

思路:

這道題就是簡單的數學題,只不過被n維嚇住了而已,只需要按照二維的思路處理n維即可。

程式碼:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
const int N = 105;
const int M = 2005;
int o[N];//圓點座標
double d[M];//點到圓點距離
double rd[M];//點的切線距離
int p[M][N];//點的座標 
int n,m; double r; double ans[M]; inline double sqr(double x){ return x * x; } int main(){ cin.tie(0); ios::sync_with_stdio(false); cin>>n>>m; cin>>r; for(int i = 0;i<n;i++){ cin>>o[i]; } for(int i = 0;i<m;i++){ double s = 0;
for(int j = 0;j<n;j++){ cin>>p[i][j]; s += sqr(p[i][j] - o[j]); } d[i] = sqrt(s);//求出點到圓心距離 rd[i] = sqrt(s - sqr(r));//求出切線距離 } for(int i = 0;i<m;i++){ for(int j = 0;j<i;j++){ double s = 0; for(int k = 0;k<n;k++){ s
+= sqr(p[i][k] - p[j][k]); } double c = sqrt(s); double a = d[i]; double b = d[j]; double t = (a + b + c) / 2; double area = sqrt(t * (t - a) * (t - b) * (t - c)); double h = 2 * area / c; if(h >= r || sqr(a) >= sqr(b) + s || sqr(b) >= sqr(a) + s){ ans[i] += c, ans[j] += c; continue; } double angle = acos((sqr(a) + sqr(b) - sqr(c)) / (2 * a * b)); double angle1 = acos( r / a); double angle2 = acos( r / b); double res = (angle - angle1 - angle2) * r + rd[i] + rd[j]; ans[i] += res, ans[j] += res; } } for(int i = 0;i<m;i++){ printf("%.13f\n", ans[i]); } }