首页 > 代码库 > HDU2948Geometry Darts(简单计算几何)
HDU2948Geometry Darts(简单计算几何)
题目大意就是说两个人掷飞镖,飞镖在所给定的图形内就记一分,现在给定N个图形(圆、三角形和矩形),问每一次比赛(没人分别掷三次)谁赢。
1 #include <map> 2 #include <set> 3 #include <stack> 4 #include <queue> 5 #include <cmath> 6 #include <ctime> 7 #include <vector> 8 #include <cstdio> 9 #include <cctype> 10 #include <cstring> 11 #include <cstdlib> 12 #include <iostream> 13 #include <algorithm> 14 using namespace std; 15 #define eps 1e-12 16 #define MAXN 55 17 #define INF 1e30 18 #define mem0(a) memset(a,0, sizeof(a)) 19 #define mem1(a) memset(a,-1,sizeof(a)) 20 double MAX(double a, double b) {return a > b ? a : b;} 21 double MIn(double a, double b) {return a < b ? a : b;} 22 typedef long long LL; 23 /****************************************计算几何头文件**************************************************/ 24 struct Point{ 25 double x,y; 26 Point(double x=0, double y=0):x(x),y(y){} 27 }; 28 29 struct Polygon 30 { 31 Point p[MAXN]; 32 int Size; 33 }; 34 35 struct Circle 36 { 37 Point o; 38 double r; 39 Circle(){} 40 Circle(Point _o, double _r):o(_o),r(_r){} 41 }; 42 43 Point operator + (Point A, Point B) {return Point(A.x+B.x, A.y+B.y);} 44 45 Point operator - (Point A, Point B) {return Point(A.x-B.x, A.y-B.y);} 46 47 Point operator * (Point A, double p) {return Point(A.x*p, A.y*p);} 48 49 Point operator / (Point A, double p) {return Point(A.x/p, A.y/p);} 50 51 int dcmp(double x) { 52 if(fabs(x) < eps) return 0; 53 else return x < 0 ? -1 : 1; 54 } 55 56 bool operator == (const Point &A, const Point &B) { 57 return dcmp(A.x-B.x) == 0 && dcmp(A.y-B.y) == 0; 58 } 59 60 double Dot(Point A, Point B) { return A.x*B.x + B.y*B.y;} //点积 61 62 double Length(Point A) { return sqrt(Dot(A,A));} //向量长度 63 64 double Angle(Point A, Point B) {return acos(Dot(A,B) / Length(A) / Length(B));}//向量夹角 65 66 double cross(Point A, Point B) {return A.x*B.y - A.y*B.x;} 67 68 bool crossed(Point a, Point b, Point c, Point d)//线段ab和cd是否相交 69 { 70 if(cross(a-c, d-c)*cross(b-c, d-c)<=0 && cross(c-a, b-a)*cross(d-a, b-a)<=0) 71 { 72 return true; 73 } 74 return false; 75 } 76 77 bool isPointOnSegent(Point p, Point s, Point e)//判断点是否在线段se上 78 { 79 double d = (p.x-s.x) * (e.x-p.x); 80 double a = (p.y-s.y) / (p.x-s.x); 81 double b = (e.y-p.y) / (e.x-p.x); 82 if(dcmp(d)==1 && dcmp(a-b)==0)return true; 83 return false; 84 } 85 86 int isPointInPolygon(Point p, Polygon poly)//判断点是否在多边形以内 87 { 88 int w = 0; 89 int n = poly.Size; 90 for(int i=0;i<n;i++) 91 { 92 if(isPointOnSegent(p, poly.p[i], poly.p[(i+1)%n])) return 1;//点在边上 93 int k = dcmp(cross(poly.p[(i+1)%n]-poly.p[i], p-poly.p[i])); 94 int d1 = dcmp(poly.p[i].y - p.y); 95 int d2 = dcmp(poly.p[(i+1)%n].y - p.y); 96 if(k > 0 && d1 <= 0 && d2 > 0) w++; 97 if(k < 0 && d2 <= 0 && d1 > 0) w--; 98 } 99 if(w != 0) return 1; 100 return 0; 101 } 102 103 /****************************************************************************************************/ 104 struct R 105 { 106 Point a, b; 107 R(){} 108 R(Point _a, Point _b) 109 { 110 a = _a; 111 b = _b; 112 } 113 }r[1001]; 114 struct T 115 { 116 Point a, b, c; 117 T(){} 118 T(Point _a, Point _b, Point _c) { 119 a = _a; b = _b; c = _c; 120 } 121 }t[1001]; 122 Circle c[1001]; 123 int S, N; 124 int cntC = 0, cntT = 0, cntR = 0; 125 126 int calc(double x, double y)//计算点(x, y)在图中的多少个图形内部 127 { 128 Point p = Point(x, y); 129 int ans = 0; 130 for(int i=0;i<cntC;i++) 131 { 132 if(Length(p-c[i].o) <= c[i].r) ans ++; 133 } 134 for(int i=0;i<cntT;i++) 135 { 136 if( cross(t[i].c-t[i].a, p-t[i].a)*cross(t[i].b-t[i].a, p-t[i].a)<=0 137 && cross(t[i].a-t[i].b, p-t[i].b)*cross(t[i].c-t[i].b, p-t[i].b)<=0 ) ans ++; 138 } 139 for(int i=0;i<cntR;i++) 140 { 141 if(x>=r[i].a.x&&x<=r[i].b.x && y>=r[i].a.y&&y<=r[i].b.y) ans ++; 142 } 143 return ans; 144 } 145 146 int main() 147 { 148 char ch;double x, y, rr; 149 while(~scanf("%d%*c", &S)) 150 { 151 cntT = cntC = cntR = 0; 152 for(int i=0;i<S;i++) 153 { 154 scanf("%c", &ch); 155 if(ch == ‘C‘) 156 { 157 scanf("%lf %lf %lf%*c", &x, &y, &rr); 158 c[cntC++] = Circle(Point(x, y), rr); 159 continue; 160 } 161 else if(ch == ‘T‘) 162 { 163 Point aa[3]; 164 for(int j=0;j<3;j++) 165 { 166 scanf("%lf%*c%lf%*c", &x, &y); 167 aa[j] = Point(x, y); 168 } 169 t[cntT++] = T(aa[0],aa[1],aa[2]); 170 } 171 else 172 { 173 Point aa[2]; 174 for(int j=0;j<2;j++) 175 { 176 scanf("%lf%*c%lf%*c", &x, &y); 177 aa[j] = Point(x, y); 178 } 179 r[cntR++] = R(aa[0], aa[1]); 180 } 181 } 182 scanf("%d", &N); 183 for(int i=0;i<N;i++) 184 { 185 int cntA = 0, cntB = 0; 186 for(int j=0;j<3;j++) 187 { 188 scanf("%lf %lf", &x, &y); 189 cntA += calc(x, y); 190 } 191 for(int j=0;j<3;j++) 192 { 193 scanf("%lf %lf", &x, &y); 194 cntB += calc(x, y); 195 } 196 // printf("%d %d\n", cntA, cntB); 197 printf("%s\n", cntA > cntB ? "Bob" : (cntA == cntB ? "Tied" : "Hannah")); 198 } 199 } 200 return 0; 201 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。