poj 1263 Reflections (Simple Geometry)
简单计算几何。题目给出射线以及若干个不相交的圆,求出射线会在哪些圆上反弹,依次写出反弹球的编号。
代码如下:
#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)的更多相关文章
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- poj 3468:A Simple Problem with Integers(线段树,区间修改求和)
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 58269 ...
- 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 ...
- POJ 3922 A simple stone game
题目: E - A simple stone game Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d &am ...
- POJ 3468 A Simple Problem with Integers //线段树的成段更新
A Simple Problem with Integers Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 59046 ...
- [POJ] 3468 A Simple Problem with Integers [线段树区间更新求和]
A Simple Problem with Integers Description You have N integers, A1, A2, ... , AN. You need to deal ...
随机推荐
- vuex的简单教程
首先安装vuex npm install vuex --save 然后创建store.js文件里写 import Vue from 'vue' import Vuex from 'vuex' Vue. ...
- cron服务
相对与at,cron的优点就是能够周期性的执行某个命令,at却只能执行一次,cron的后台进程名字是crond ,cron也是system V的服务,所以我们可以service crond start ...
- 集训队日常训练20180525-DIV1
A.2805 N*M的图,每次浇水(X1,Y1)-(X2,Y2)围成的矩形,问最后有多少点被浇水了. 暴力. #include<bits/stdc++.h> using namespace ...
- CommonJS、requirejs、ES6的对比
文件路径 首先先搞清楚文件路径的写法,这里我总是记不住,有点晕,正好这次整理一下. 以 / 为起始,表示从根目录开始解析: 以 ./ 为起始,表示从当前目录开始解析: 以 ../ 为起始,表示从上级目 ...
- HDU 4006优先队列
//按照降序排列,而且队列中只保存k个元素 #include<stdio.h> #include<queue> using namespace std; int main(){ ...
- 模拟退火解TSP问题MATLAB代码
分别把前四个函数存成m文件,再运行最后一个. swap.m function [ newpath , position ] = swap( oldpath , number ) % 对 oldpath ...
- docker.[7] 数据卷容器
docker.[7] 数据卷容器 挂在数据卷容器的方法: # docker run --volumes-from [Container Name]
- objectarx之判断给定的三点是否共线
bool ThreePointIsCollinear(const AcGePoint2d &pt1, const AcGePoint2d &pt2, const AcGePoint2d ...
- SDUT-3404_数据结构实验之排序七:选课名单
数据结构实验之排序七:选课名单 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 随着学校规模的扩大,学生人数急剧增加,选 ...
- npm run dev 和 npm run serve
1.ERR引发的思考 创建好的 vue 项目直接执行 vue run dev 报错?运行 vue run serve 就可以启动...如下 npm run dev npm ERR! missing s ...