HDU5572 2015上海现场赛A题1001

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<queue>
  5 #include<map>
  6 #include<set>
  7 #include<cmath>
  8 using namespace std;
  9 typedef long long LL;
 10 const double eps = 1e-8;
 11 int sgn(double x) {
 12     if(fabs(x) < eps) return 0;
 13     if(x < 0) return -1;
 14     else return 1;
 15 }
 16 struct Point {
 17     double x, y;
 18     Point(double x = 0, double y = 0) : x(x), y(y) {}
 19     Point operator - (const Point& t) const {
 20         return Point(x - t.x, y - t.y);
 21     }
 22     double operator ^ (const Point& t) const {
 23         return x * t.y - y * t.x;
 24     }
 25     double operator * (const Point& t) const {
 26         return x * t.x + y * t.y;
 27     }
 28     Point operator * (const double& k) const {
 29         return Point(x * k, y * k);
 30     }
 31     Point operator / (const double& k) const {
 32         return Point(x / k, y / k);
 33     }
 34     Point operator + (const Point& t) const {
 35         return Point(x + t.x, y + t.y);
 36     }
 37     double distance(Point p) {
 38         return hypot(x - p.x, y - p.y);
 39     }
 40     double len() {
 41         return hypot(x, y);
 42     }
 43     double len2() {
 44         return x * x + y * y;
 45     }
 46     Point trunc(double r) {
 47         double l = len();
 48         if(!sgn(l)) return *this;
 49         r /= l;
 50         return Point(x * r, y * r);
 51     }
 52 };
 53 struct Line {
 54     Point s, e;
 55     Line() {}
 56     Line(Point s, Point e) {
 57         this->s = s; this->e = e;
 58     }
 59     double length() {
 60         return sqrt((s.x - e.x) * (s.x - e.x) + (s.y - e.y) * (s.y - e.y));
 61     }
 62     int relation(Point p) {
 63         int c = sgn((p - s) ^ (e - s));
 64         if(c < 0) return 1;
 65         else if(c > 0) return 2;
 66         else return 3;
 67     }
 68     double dispointtoline(Point p) {
 69         return fabs((p - s) ^ (e - s)) / length();
 70     }
 71     double dispointtoseg(Point p) {
 72         if(sgn((p - s) * (e - s)) < 0 || sgn((p - e) * (s - e)) < 0)
 73             return min(p.distance(s), p.distance(e));
 74         return dispointtoline(p);
 75     }
 76     Point lineprog(Point p) {
 77         return s + (((e - s) * ((e - s) * (p - s))) / ((e - s).len2()));
 78     }
 79     Point symmetrypoint(Point p) {
 80         Point q = lineprog(p);
 81         return Point(2 * q.x - p.x, 2 * q.y - p.y);
 82     }
 83 };
 84 struct Circle {
 85     Point p;
 86     double r;
 87     Circle(double x, double y, double r) {
 88         p  = Point(x, y);
 89         this->r = r;
 90     }
 91     int relationline(Line v) {
 92         double dst = v.dispointtoline(p);
 93         if(sgn(dst - r) < 0) return 2;
 94         else if(sgn(dst - r) == 0) return 1;
 95         else return 0;
 96     }
 97     int relationseg(Line v) {
 98         double dst = v.dispointtoseg(p);
 99         if(sgn(dst - r) < 0) return 2;
100         else if(sgn(dst - r) == 0) return 1;
101         else return 0;
102     }
103     int pointcrossline(Line v, Point& p1, Point& p2) {
104         if(!(*this).relationline(v)) return 0;
105         Point a = v.lineprog(p);
106         double d = v.dispointtoline(p);
107         d = sqrt(r * r - d * d);
108         if(sgn(d) == 0) {
109             p1 = a; p2 = a;
110             return 1;
111         }
112         p1 = a + (v.e - v.s).trunc(d);
113         p2 = a - (v.e - v.s).trunc(d);
114         return 2;
115     }
116 };
117 int main() {
118     int T; scanf("%d", &T);
119     int ca(1);
120     while(T--) {
121         int ox, oy, r;
122         int ax, ay, vx, vy;
123         int bx, by;
124         scanf("%d%d%d", &ox, &oy, &r);
125         scanf("%d%d%d%d", &ax, &ay, &vx, &vy);
126         scanf("%d%d", &bx, &by);
127         Line l(Point(ax, ay), Point(ax + vx, ay + vy));
128         Circle c(ox, oy, r);
129         Point d(bx, by);
130         Line t(Point(ax, ay), Point(bx, by));
131         if(l.relation(d) == 3 && c.relationseg(t) <= 1 && sgn(Point(bx - ax, by - ay) ^ Point(vx, vy)) == 0) printf("Case #%d: Yes\n", ca++);
132         else {
133             Point a, b;
134             if(c.pointcrossline(l, a, b) != 2) printf("Case #%d: No\n", ca++);
135             else {
136                 Point cut;
137                 if(a.distance(Point(ax, ay)) > b.distance(Point(ax, ay))) cut = b;
138                 else cut = a;
139                 Line mid(cut, c.p);
140                 Point en = mid.symmetrypoint(Point(ax, ay));
141                 Line aft(cut, en);
142                 if(aft.e.distance(d) > aft.s.distance(d)) swap(aft.s, aft.e);
143                 if(sgn((aft.e - aft.s) ^ Point(d.x - cut.x, d.y - cut.y)) == 0) printf("Case #%d: Yes\n", ca++);
144                 else printf("Case #%d: No\n", ca++);
145             }
146         }
147     }
148     return 0;
149 }

醉了,,现场读错题了,分反弹和不反弹两种情况讨论就好了,加点特判

posted @ 2015-12-02 21:41  不会起昵称  阅读(492)  评论(0编辑  收藏  举报