18.12.25 POJ 1228 Grandpa's Estate
描述
Being the only living descendant of his grandfather, Kamran the Believer inherited all of the grandpa's belongings. The most valuable one was a piece of convex polygon shaped farm in the grandpa's birth village. The farm was originally separated from the neighboring farms by a thick rope hooked to some spikes (big nails) placed on the boundary of the polygon. But, when Kamran went to visit his farm, he noticed that the rope and some spikes are missing. Your task is to write a program to help Kamran decide whether the boundary of his farm can be exactly determined only by the remaining spikes.输入The first line of the input file contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case contains an integer n (1 <= n <= 1000) which is the number of remaining spikes. Next, there are n lines, one line per spike, each containing a pair of integers which are x and y coordinates of the spike.
输出
There should be one output line per test case containing YES or NO depending on whether the boundary of the farm can be uniquely determined from the input.
样例输入
1
6
0 0
1 2
3 4
2 0
2 4
5 0
样例输出
NO
来源
Tehran 2002 Preliminery
题解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include <string.h> 3 #include <algorithm> 4 #include <stack> 5 #include <string> 6 #include <math.h> 7 #include <queue> 8 #include <stdio.h> 9 #include <string.h> 10 #include <set> 11 #include <vector> 12 #define maxn 1005 13 #define inf 999999 14 using namespace std; 15 16 int n; 17 struct node { 18 int x, y, idx; 19 }spike[maxn]; 20 bool operator <(node a, node b) { 21 if (a.x == b.x) 22 return a.y < b.y; 23 return a.x < b.x; 24 } 25 int bao[maxn], c = 0; 26 bool in[maxn]; 27 bool vis[maxn]; 28 29 void solve() { 30 deque<node>all; 31 all.push_back(spike[1]), all.push_back(spike[2]); 32 for (int i = 3; i <= n; i++) { 33 while (1) { 34 int size = all.size(); 35 if (size <= 1) { 36 all.push_back(spike[i]); 37 break; 38 } 39 node t = all[size-1],u = all[size-2]; 40 node ut, ta; 41 ut.x = t.x - u.x; ut.y = t.y - u.y; 42 ta.x = spike[i].x - t.x; ta.y = spike[i].y - t.y; 43 int tmp = ut.x*ta.y - ut.y*ta.x; 44 if (tmp < 0) { 45 in[all.back().idx] = false; 46 all.pop_back(); 47 } 48 else { 49 if (tmp == 0) 50 in[all.back().idx] = true; 51 all.push_back(spike[i]); 52 break; 53 } 54 } 55 } 56 while (!all.empty()) { 57 bao[++c] = all.front().idx; 58 vis[bao[c]] = true; 59 all.pop_front(); 60 } 61 for (int i = 1; i <= c-1; i++) { 62 if (in[bao[i]] ==false&& in[bao[i + 1]]==false) { 63 printf("NO\n"); 64 return; 65 } 66 } 67 all.push_back(spike[n]), all.push_back(spike[n-1]); 68 for (int i = n-2; i >= 1; i--) { 69 while (1) { 70 int size = all.size(); 71 if (size <= 1) { 72 all.push_back(spike[i]); 73 break; 74 } 75 node t = all[size - 1], u = all[size - 2]; 76 node ut, ta; 77 ut.x = t.x - u.x; ut.y = t.y - u.y; 78 ta.x = spike[i].x - t.x; ta.y = spike[i].y - t.y; 79 int tmp = ut.x*ta.y - ut.y*ta.x; 80 if (tmp < 0) { 81 in[all.back().idx] = false; 82 all.pop_back(); 83 } 84 else { 85 if (tmp == 0) 86 in[all.back().idx] = true; 87 all.push_back(spike[i]); 88 break; 89 } 90 } 91 } 92 all.pop_front(); 93 c = 0; 94 while (!all.empty()) { 95 bao[++c] = all.front().idx; 96 all.pop_front(); 97 if (vis[bao[c]]&&!all.empty()) { 98 printf("NO\n"); 99 return; 100 } 101 } 102 for (int i = 1; i <= c-1; i++) { 103 if (in[bao[i]] ==false&& in[bao[i + 1]]==false) { 104 printf("NO\n"); 105 return; 106 } 107 } 108 printf("YES\n"); 109 } 110 111 void init() { 112 scanf("%d", &n); 113 memset(in, 0, sizeof(in)); 114 memset(vis, 0, sizeof(vis)); 115 c = 0; 116 for (int i = 1; i <= n; i++) { 117 scanf("%d%d", &spike[i].x, &spike[i].y); 118 spike[i].idx = i; 119 } 120 sort(spike + 1, spike + 1 + n); 121 solve(); 122 } 123 124 int main() { 125 int kase; 126 scanf("%d", &kase); 127 while(kase--) 128 init(); 129 return 0; 130 }
做这题的时候我还觉得计算几何没什么大不了的……
直到我看到了后两道……
我不该在课上做其他科作业的……