1263 -- Reflections

  简单计算几何。题目给出射线以及若干个不相交的圆,求出射线会在哪些圆上反弹,依次写出反弹球的编号。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector> using namespace std; const double EPS = 1e-;
template<class T> T sqr(T x) { return x * x;}
inline int sgn(double x) { return (x > EPS) - (x < -EPS);}
struct Point {
double x, y;
Point() {}
Point(double x, double y) : x(x), y(y) {}
bool operator < (Point a) const { return sgn(x - a.x) < || sgn(x - a.x) == && y < a.y;}
bool operator == (Point a) const { return sgn(x - a.x) == && sgn(y - a.y) == ;}
Point operator + (Point a) { return Point(x + a.x, y + a.y);}
Point operator - (Point a) { return Point(x - a.x, y - a.y);}
Point operator * (double p) { return Point(x * p, y * p);}
Point operator / (double p) { return Point(x / p, y / p);}
} ;
typedef Point Vec; inline double cross(Vec a, Vec b) { return a.x * b.y - a.y * b.x;}
inline double cross(Point o, Point a, Point b) { return cross(a - o, b - o);}
inline double dot(Vec a, Vec b) { return a.x * b.x + a.y * b.y;}
inline double dot(Point o, Point a, Point b) { return dot(a - o, b - o);}
inline double veclen(Vec x) { return sqrt(dot(x, x));}
inline Vec normal(Vec x) { return Vec(-x.y, x.x) / veclen(x);} struct Line {
Point s, t;
Line() {}
Line(Point s, Point t) : s(s), t(t) {}
Vec vec() { return t - s;}
Point pt(double x) { return s + vec() * x;}
Line move(double x) {
Vec nor = normal(vec());
return Line(s + nor * x, t + nor * x);
}
} ; inline Point llint(Point P, Vec v, Point Q, Vec w) { return P + v * (cross(w, P - Q) / cross(v, w));}
inline Point llint(Line a, Line b) { return llint(a.s, a.vec(), b.s, b.vec());} struct Circle {
Point c;
double r;
Circle() {}
Circle(Point c, double r) : c(c), r(r) {}
} ; void lcint(Line L, Circle C, vector<Point> &sol) {
Point ip = llint(L, Line(C.c, C.c + normal(L.vec())));
double dis = veclen(ip - C.c);
if (sgn(dis - C.r) >= ) return ;
Vec u = L.vec() / veclen(L.vec());
double d = sqrt(sqr(C.r) - sqr(dis));
sol.push_back(ip + u * d);
sol.push_back(ip - u * d);
} Point reflect(Point x, Line L) {
Vec nor = normal(L.vec());
Point ip = llint(L, Line(x, x + nor));
return ip + ip - x;
} vector<Circle> rec;
Point src;
Vec dir; const double FINF = 1e100;
inline bool onCircle(Point p, Circle c) { return sgn(veclen(p - c.c) - c.r) == ;} void work() {
int cnt = , sz = rec.size();
vector<Point> tmp;
Point ip;
while (true) {
tmp.clear();
double d = FINF;
for (int i = ; i < sz; i++) lcint(Line(src, src + dir), rec[i], tmp);
for (int i = , sz = tmp.size(); i < sz; i++) {
double t = (tmp[i].x - src.x) / dir.x;
if (t > EPS) d = min(d, t);
}
if (sgn(d - FINF) >= ) break;
cnt++;
if (cnt > ) break;
ip = src + dir * d;
int mk = -;
for (int i = ; i < sz; i++) if (onCircle(ip, rec[i])) { mk = i; break;}
if (mk == -) { puts("shit!!"); while () ;}
printf("%d ", mk + );
dir = reflect(src, Line(ip, rec[mk].c)) - ip;
src = ip;
}
if (cnt > ) puts("...");
else puts("inf");
} int main() {
// freopen("in", "r", stdin);
int cas = , n;
double x, y, r;
while (cin >> n && n) {
rec.clear();
while (n--) {
cin >> x >> y >> r;
rec.push_back(Circle(Point(x, y), r));
}
cin >> src.x >> src.y;
cin >> dir.x >> dir.y;
dir = dir / veclen(dir);
printf("Scene %d\n", cas++);
work();
puts("");
}
return ;
}

  速度好慢,整整写了一个小时。对几何模板还是不算非常熟悉,虽然已经能够灵活写出部分基础函数了,但是速度还真是一个大问题。最后结果,因为手多血多个换行PE了一次,然后就AC了~

——written by Lyon

poj 1263 Reflections (Simple Geometry)的更多相关文章

  1. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  2. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  3. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  4. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  5. poj 3468:A Simple Problem with Integers(线段树,区间修改求和)

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 58269   ...

  6. poj 3468 A Simple Problem with Integers 线段树第一次 + 讲解

    A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal w ...

  7. POJ 3922 A simple stone game

    题目: E - A simple stone game Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d &am ...

  8. POJ 3468 A Simple Problem with Integers //线段树的成段更新

    A Simple Problem with Integers Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 59046   ...

  9. [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]

    A Simple Problem with Integers   Description You have N integers, A1, A2, ... , AN. You need to deal ...

随机推荐

  1. 在Linux系统下进入MySql数据库进行操作

    例:   ---- 1.进入mysql数据库 root@test:/home# mysql -uroot -proot   <uroot是用户名,proot是密码> 2.查询所有的库 my ...

  2. day18 9.转账汇款案例(1)

  3. Jquery选择器分类:基本选择器,层次选择器,过滤选择器,表单选择器。

    基本选择器 说明:通过元素id.class和标签名等来查找DOM元素 1.id选择器:$("#test");//选取id为test的元素 2.类选择器:$(".test& ...

  4. jquery 设置 html标签响应式布局

    function sWidth() {//计算当前设备宽度 var widthSize; if ($(window).width() <= 640) { widthSize = $(window ...

  5. IDEA 创建文件夹总默认根节点问题解决

    上面是文件夹结构显示,如果勾掉,就是按层级显示,空目录不会自动折叠成一行 原文地址;https://blog.csdn.net/huangjunwei6/article/details/7150755 ...

  6. day38 14-Spring的Bean的属性的注入:集合属性的注入

    集合:List.Set.Map. package cn.itcast.spring3.demo6; import java.util.List; import java.util.Map; impor ...

  7. JQuery--jQquery控制CSS样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. IE8下的兼容小经验

    placeholder IE8下不支持HTML5属性placeholder,不过为解决此问题的js插件挺多的,比如:jquery-placeholder.也可以使用jquery来写. last-chi ...

  9. 【软件安装】我喜欢的notepad插件

    1.文件管理器 explorer 2.16进制查看文件工具 HEX-Editor

  10. Adapter小练习

    Aapter的继承关系图: Android中Adapter的是数据和视图之间的桥梁,数据在adapter中做处理,然后显示到视图上面. 一.ArrayAdapter适配器 java代码: import ...