两题都是判断多边形的核。。直接套用模板。。
code(poj3130):
1 /* 2 State:Accepted 3 Time:2013-04-11 19:04:36 4 */ 5 #include<iostream> 6 #include<cstring> 7 #include<cstdio> 8 #include<cmath> 9 #include<algorithm> 10 #include<set> 11 #define eps 1e-9 12 #define maxn 210 13 using namespace std; 14 15 int ln, q[maxn], top, bot, n, ord[maxn], T; 16 double maxdist; 17 struct point{double x, y; } p[maxn]; 18 struct line { 19 point a, b; 20 double angle; 21 } ; 22 line l[maxn], l1[maxn]; 23 24 void add_line(double x1, double y1, double x2, double y2 ){ 25 l[ln].a.x = x1; 26 l[ln].b.x = x2; 27 l[ln].a.y = y1; 28 l[ln].b.y = y2; 29 l[ln].angle = atan2(y2 - y1, x2 - x1); 30 ord[ln] = ln; 31 ++ln; 32 } 33 34 void init(){ 35 double x1 , y1, x2, y2, dd; 36 for (int i =0; i < n; ++i){ 37 scanf("%lf%lf",&x1, &y1); 38 p[i].x = x1; 39 p[i].y = y1; 40 } 41 ln = 0; 42 p[n] = p[0]; 43 for (int i = 0; i < n; ++i){ 44 add_line(p[i].x, p[i].y, p[i+1].x, p[i+1].y); 45 } 46 47 48 } 49 50 int dblcmp(double k){ 51 if (fabs(k) < eps) return 0; 52 return k > 0 ? 1 : -1; 53 } 54 55 double multi(point p0, point p1, point p2){ 56 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); 57 } 58 59 bool cmp(const int u, const int v){ 60 int d = dblcmp(l[u].angle - l[v].angle); 61 if (d == 0) return dblcmp(multi(l[u].a, l[v].a,l[v].b)) > 0; 62 return d < 0; 63 } 64 65 void get_point(line l1, line l2, point& p){ 66 double dot1, dot2; 67 dot1 = multi(l2.a, l1.b, l1.a); 68 dot2 = multi(l1.b, l2.b, l1.a); 69 p.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot1 + dot2); 70 p.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot1 + dot2); 71 } 72 73 bool judge(line l0, line l1, line l2){ 74 point p; 75 get_point(l1, l2, p); 76 return dblcmp(multi(p, l0.a , l0.b)) < 0; 77 } 78 79 80 bool SAI(){ 81 sort(ord, ord + ln , cmp); 82 int i, j; 83 for (i = 0, j = 0; i < ln; ++i) 84 if (dblcmp(l[ord[i]].angle - l[ord[j]].angle) > 0) 85 ord[++j] = ord[i]; 86 ln = j + 1; 87 q[0] = ord[0]; 88 q[1] = ord[1]; 89 bot = 0; 90 top = 1; 91 for (int i = 2; i < ln ; ++i){ 92 while (bot < top && judge(l[ord[i]], l[q[top - 1]], l[q[top]])) --top; 93 while (bot < top && judge(l[ord[i]], l[q[bot + 1]], l[q[bot]])) ++bot; 94 q[++top] = ord[i]; 95 } 96 while (bot < top && judge(l[q[bot]], l[q[top-1]], l[q[top]])) --top; 97 while (bot < top && judge(l[q[top]], l[q[bot+1]], l[q[bot]])) ++bot; 98 if (top - bot >= 2) return 1; 99 else return 0; 100 } 101 102 int main(){ 103 freopen("poj3130.in","r",stdin); 104 freopen("poj3130.out","w",stdout); 105 while (scanf("%d", &n) && n){ 106 init(); 107 if (SAI()) printf("1\n"); 108 else printf("0\n"); 109 } 110 fclose(stdin); fclose(stdout); 111 }
poj3335
1 /* 2 State:Accepted 3 Time:2013-04-11 19:06:14 4 */ 5 #include<iostream> 6 #include<cstring> 7 #include<cstdio> 8 #include<cmath> 9 #include<algorithm> 10 #include<set> 11 #define eps 1e-9 12 #define maxn 210 13 using namespace std; 14 15 int ln, q[maxn], top, bot, n, ord[maxn], T; 16 double maxdist; 17 struct point{double x, y; } p[maxn]; 18 struct line { 19 point a, b; 20 double angle; 21 } ; 22 line l[maxn], l1[maxn]; 23 24 void add_line(double x1, double y1, double x2, double y2 ){ 25 l[ln].a.x = x1; 26 l[ln].b.x = x2; 27 l[ln].a.y = y1; 28 l[ln].b.y = y2; 29 l[ln].angle = atan2(y2 - y1, x2 - x1); 30 ord[ln] = ln; 31 ++ln; 32 } 33 34 void init(){ 35 double x1 , y1, x2, y2, dd; 36 scanf("%d", &n); 37 for (int i =0; i < n; ++i){ 38 scanf("%lf%lf",&x1, &y1); 39 p[i].x = x1; 40 p[i].y = y1; 41 } 42 ln = 0; 43 p[n] = p[0]; 44 for (int i = 0; i < n; ++i){ 45 add_line(p[i+1].x, p[i+1].y, p[i].x, p[i].y); 46 } 47 48 49 } 50 51 int dblcmp(double k){ 52 if (fabs(k) < eps) return 0; 53 return k > 0 ? 1 : -1; 54 } 55 56 double multi(point p0, point p1, point p2){ 57 return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x); 58 } 59 60 bool cmp(const int u, const int v){ 61 int d = dblcmp(l[u].angle - l[v].angle); 62 if (d == 0) return dblcmp(multi(l[u].a, l[v].a,l[v].b)) > 0; 63 return d < 0; 64 } 65 66 void get_point(line l1, line l2, point& p){ 67 double dot1, dot2; 68 dot1 = multi(l2.a, l1.b, l1.a); 69 dot2 = multi(l1.b, l2.b, l1.a); 70 p.x = (l2.a.x * dot2 + l2.b.x * dot1) / (dot1 + dot2); 71 p.y = (l2.a.y * dot2 + l2.b.y * dot1) / (dot1 + dot2); 72 } 73 74 bool judge(line l0, line l1, line l2){ 75 point p; 76 get_point(l1, l2, p); 77 return dblcmp(multi(p, l0.a , l0.b)) < 0; 78 } 79 80 81 bool SAI(){ 82 sort(ord, ord + ln , cmp); 83 int i, j; 84 for (i = 0, j = 0; i < ln; ++i) 85 if (dblcmp(l[ord[i]].angle - l[ord[j]].angle) > 0) 86 ord[++j] = ord[i]; 87 ln = j + 1; 88 q[0] = ord[0]; 89 q[1] = ord[1]; 90 bot = 0; 91 top = 1; 92 for (int i = 2; i < ln ; ++i){ 93 while (bot < top && judge(l[ord[i]], l[q[top - 1]], l[q[top]])) --top; 94 while (bot < top && judge(l[ord[i]], l[q[bot + 1]], l[q[bot]])) ++bot; 95 q[++top] = ord[i]; 96 } 97 while (bot < top && judge(l[q[bot]], l[q[top-1]], l[q[top]])) --top; 98 while (bot < top && judge(l[q[top]], l[q[bot+1]], l[q[bot]])) ++bot; 99 if (top - bot >= 2) return 1; 100 else return 0; 101 } 102 103 int main(){ 104 freopen("poj3335.in","r",stdin); 105 freopen("poj3335.out","w",stdout); 106 scanf("%d",&T); 107 for (int i =1; i <= T; ++i){ 108 init(); 109 if (SAI()) printf("YES\n"); 110 else printf("NO\n"); 111 } 112 fclose(stdin); fclose(stdout); 113 }