SGU110 Dungeon

SGU110 Dungeon

题目大意

空间内有N个表面反光的球体,从给定的起点(严格在球外)按照某一方向射出一道激光
依次输出激光射中的球体的编号,若超过10个则只输出前10个,并以”etc.”结尾

算法思路

通过计算几何模拟光的路径
每次枚举所有的球,求射线与球的交点,取最近的交点作为反射点
将起点更新为反射点,向量则变为其关于反射点法向量的对称向量的反向量

时间复杂度: O(10N)

代码

/**
 * Copyright (c) 2015 Authors. All rights reserved.
 * 
 * FileName: 110.cpp
 * Author: Beiyu Li <sysulby@gmail.com>
 * Date: 2015-05-22
 */
#include <bits/stdc++.h>

using namespace std;

#define rep(i,n) for (int i = 0; i < (n); ++i)
#define For(i,s,t) for (int i = (s); i <= (t); ++i)
#define foreach(i,c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)

typedef long long LL;
typedef pair<int, int> Pii;

const int inf = 0x3f3f3f3f;
const LL infLL = 0x3f3f3f3f3f3f3f3fLL;

const double eps = 1e-8;
int sgn(double x) { return x < -eps? -1: x > eps; }

struct Point3 {
        double x, y, z;
        Point3(double x = 0, double y = 0, double z = 0): x(x), y(y), z(z) {}
};
typedef Point3 Vector3;
Vector3 operator-(Vector3 v)
{ return Vector3(-v.x, -v.y, -v.z); }
Vector3 operator+(Vector3 u, Vector3 v)
{ return Vector3(u.x + v.x, u.y + v.y, u.z + v.z); }
Vector3 operator-(Vector3 u, Vector3 v)
{ return Vector3(u.x - v.x, u.y - v.y, u.z - v.z); }
Vector3 operator*(Vector3 u, double k)
{ return Vector3(u.x * k, u.y * k, u.z * k); }
Vector3 operator/(Vector3 u, double k)
{ return Vector3(u.x / k, u.y / k, u.z / k); }

double dot(Vector3 u, Vector3 v) { return u.x * v.x + u.y * v.y + u.z * v.z; }
double abs(Vector3 v) { return sqrt(dot(v, v)); }
Vector3 unit(Vector3 v) { return v / abs(v); }
Vector3 proj(Vector3 u, Vector3 v) { return v * dot(u, v) / dot(v, v); }
Vector3 reflect(Vector3 u, Vector3 v) { return proj(u, v) * 2.0 - u; }

const int maxn = 50 + 5;

int n;
Point3 c[maxn];
double r[maxn];
Point3 p;
Vector3 v;

void solve()
{
        rep(j,11) {
                Point3 t;
                int k = -1;
                rep(i,n) {
                        Point3 o = p + proj(c[i] - p, v);
                        double d = abs(o - c[i]);
                        if (sgn(d - r[i]) > 0) continue;
                        o = o - unit(v) * sqrt(r[i] * r[i] - d * d);
                        double dt = dot(o - p, v);
                        if (sgn(dt) <= 0) continue;
                        if (k == -1 || dt < dot(t - p, v)) t = o, k = i;
                }
                if (k == -1) { puts(""); return; }
                if (j == 10) { puts(" etc."); return; }
                p = t; v = reflect(-v, t - c[k]);
                printf("%s%d", j? " ": "", k + 1);
        }
}

int main()
{
        scanf("%d", &n);
        double x, y, z;
        rep(i,n) {
                scanf("%lf%lf%lf%lf", &x, &y, &z, &r[i]);
                c[i] = Point3(x, y, z);
        }
        scanf("%lf%lf%lf", &x, &y, &z);
        p = Point3(x, y, z);
        scanf("%lf%lf%lf", &x, &y, &z);
        v = Point3(x, y, z) - p;
        solve();

        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值