http://poj.org/problem?id=1228
看完凸包,第一次写凸包的题,搞了一晚终于AC了。
这篇博客只是记录一下代码...
View Code
1 //#include <iostream> 2 #include <cstdio> 3 #include <cstdlib> 4 #include <cstring> 5 #include <cmath> 6 #include <cctype> 7 #include <vector> 8 #include <set> 9 #include <map> 10 #include <string> 11 #include <algorithm> 12 #include <stack> 13 #include <queue> 14 15 #define INF 0x7fffffff 16 #define reset(a) memset(a, 0, sizeof(a)) 17 #define copy(a, b) memcpy(a, b, sizeof(b)) 18 #define FMAX (1E300) 19 #define MAX 1000000000 20 #define feq(a, b) (fabs((a)-(b))<1E-6) 21 #define flq(a, b) ((a)<(b)||feq(a, b)) 22 #define BASE 137 23 #define PASS puts("pass") 24 #define filein freopen("training_test.in", "r", stdin) 25 #define fileout freopen("training_test.out", "w", stdout) 26 #define eps 1e-8 27 #define max2(a, b) ((a) > (b) ? (a) : (b)) 28 #define min2(a, b) ((a) < (b) ? (a) : (b)) 29 #define MAXN 1000001 30 //#define __int64 long long 31 //#define I64d lld 32 33 #define debug 0 34 35 using namespace std; 36 37 struct point{ 38 double x; 39 double y; 40 bool operator < (const point &a) const{ 41 if (!feq(x, a.x)) return x < a.x; 42 return y < a.y; 43 } 44 bool operator == (const point &a) const{ 45 return feq(x, a.x) && feq(y, a.y); 46 } 47 point operator - (point &a) const{ 48 point tmp; 49 tmp.x = x - a.x; 50 tmp.y = y - a.y; 51 return tmp; 52 } 53 }p[1001], s[1001]; 54 point mp; 55 int top; 56 57 double dis(point a, point b){ 58 return abs(a.x - b.x) + abs(a.y - b.y); 59 } 60 61 double det(point a, point b){ 62 return a.x * b.y - b.x * a.y; 63 } 64 65 int cross(point a, point b, point c){ 66 double tmp = det(a - b, b - c); 67 #if debug 68 printf("tmp %.5f\n", tmp); 69 #endif 70 return fabs(tmp) > eps ? (tmp > 0 ? 1 : -1) : 0; 71 } 72 73 bool cmp(point a, point b){ 74 double tmp = cross(mp, a, b); 75 return fabs(tmp) > eps ? (tmp > 0) : (dis(mp, a) < dis(mp,b)); 76 } 77 78 int main(){ 79 int T, n; 80 81 scanf("%d", &T); 82 while (T--){ 83 mp = {FMAX, FMAX}; 84 scanf("%d", &n); 85 for (int i = 0; i < n; i++){ 86 scanf("%lf%lf", &p[i].x, &p[i].y); 87 mp = min(mp, p[i]); 88 } 89 #if debug 90 printf("mp %.5f %.5f\n", mp.x, mp.y); 91 #endif 92 if (n <= 5){ 93 printf("NO\n"); 94 continue; 95 } 96 sort(p, p + n, cmp); 97 #if debug 98 for (int i = 0; i < n; i++){ 99 printf("%.5f %.5f\n", p[i].x, p[i].y); 100 } 101 for (int i = 0; i < n - 2; i++){ 102 printf("%d: %d\n", i, cross(p[i], p[i + 1], p[i + 2])); 103 } 104 #endif 105 106 int t = n; 107 while (t--){ 108 if (cross(mp, p[t], p[t - 1])){ 109 break; 110 } 111 } 112 for (int i = t; i <= (t + n - 1) / 2; i++){ 113 point tt; 114 115 tt = p[i]; 116 p[i] = p[n - 1 - (i - t)]; 117 p[n - 1 - (i - t)] = tt; 118 } 119 #if debug 120 for (int i = 0; i < n; i++){ 121 printf("%.5f %.5f\n", p[i].x, p[i].y); 122 } 123 for (int i = 0; i < n - 2; i++){ 124 printf("%d: %d\n", i, cross(p[i], p[i + 1], p[i + 2])); 125 } 126 #endif 127 128 bool ans = true, last = true; 129 for (int i = 0; i <= n + 2; i++){ 130 int tmp = cross(p[i % n], p[(i + 1) % n], p[(i + 2) % n]); 131 if (!tmp){ 132 #if debug 133 printf("i %d\n", i); 134 #endif 135 last = true; 136 } 137 else{ 138 if (!last){ 139 ans = false; 140 #if debug 141 printf("tmp %d\n%.2f %.2f %.2f\n%.2f %.2f %.2f\n", tmp, p[i % n].x, p[(i + 1) % n].x, p[(i + 2) % n].x, p[i % n].y, p[(i + 1) % n].y, p[(i + 2) % n].y); 142 printf("false ans %d\n", i); 143 #endif 144 break; 145 } 146 last = false; 147 } 148 } 149 if (ans) 150 printf("YES\n"); 151 else 152 printf("NO\n"); 153 } 154 155 return 0; 156 }
——written by Lyon