POJ 1696 Space Ant(凸包变形)

Description

The most exciting space discovery occurred at the end of the 20th century. In 1999, scientists traced down an ant-like creature in the planet Y1999 and called it M11. It has only one eye on the left side of its head and just three feet all on the right side of its body and suffers from three walking limitations: 
  1. It can not turn right due to its special body structure. 
  2. It leaves a red path while walking. 
  3. It hates to pass over a previously red colored path, and never does that.

The pictures transmitted by the Discovery space ship depicts that plants in the Y1999 grow in special points on the planet. Analysis of several thousands of the pictures have resulted in discovering a magic coordinate system governing the grow points of the plants. In this coordinate system with x and y axes, no two plants share the same x or y
An M11 needs to eat exactly one plant in each day to stay alive. When it eats one plant, it remains there for the rest of the day with no move. Next day, it looks for another plant to go there and eat it. If it can not reach any other plant it dies by the end of the day. Notice that it can reach a plant in any distance. 
The problem is to find a path for an M11 to let it live longest. 
Input is a set of (x, y) coordinates of plants. Suppose A with the coordinates (xA, yA) is the plant with the least y-coordinate. M11 starts from point (0,yA) heading towards plant A. Notice that the solution path should not cross itself and all of the turns should be counter-clockwise. Also note that the solution may visit more than two plants located on a same straight line. 

Input

The first line of the input is M, the number of test cases to be solved (1 <= M <= 10). For each test case, the first line is N, the number of plants in that test case (1 <= N <= 50), followed by N lines for each plant data. Each plant data consists of three integers: the first number is the unique plant index (1..N), followed by two positive integers x and y representing the coordinates of the plant. Plants are sorted by the increasing order on their indices in the input file. Suppose that the values of coordinates are at most 100.

Output

Output should have one separate line for the solution of each test case. A solution is the number of plants on the solution path, followed by the indices of visiting plants in the path in the order of their visits.
 
题目大意:从与最低点有相同纵坐标的y轴上的点出发,只能左转,问最多走过多少个点,把这些点列出来。
思路:细细斟酌,发现其实全部都能走嘛o(╯□╰)o。
像凸包构造那样走,就能走完了,其实就是一个凸包的变种o(╯□╰)o
数据范围比较小就直接O(n²)暴力算了o(╯□╰)o
 
代码(0MS):
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cmath>
 6 using namespace std;
 7 
 8 const double EPS = 1e-8;
 9 
10 inline int sgn(double x) {
11     return (x > EPS) - (x < -EPS);
12 }
13 
14 struct Point {
15     double x, y;
16     Point() {}
17     Point(double x, double y): x(x), y(y) {}
18     void read() {
19         scanf("%lf%lf", &x, &y);
20     }
21     bool operator < (const Point &rhs) const {
22         if(y != rhs.y) return y < rhs.y;
23         return x < rhs.x;
24     }
25     Point operator + (const Point &rhs) const {
26         return Point(x + rhs.x, y + rhs.y);
27     }
28     Point operator - (const Point &rhs) const {
29         return Point(x - rhs.x, y - rhs.y);
30     }
31     Point operator * (const int &b) const {
32         return Point(x * b, y * b);
33     }
34     Point operator / (const int &b) const {
35         return Point(x / b, y / b);
36     }
37     double length() const {
38         return sqrt(x * x + y * y);
39     }
40     Point unit() const {
41         return *this / length();
42     }
43 };
44 typedef Point Vector;
45 
46 double dist(const Point &a, const Point &b) {
47     return (a - b).length();
48 }
49 
50 double across(const Point &a, const Point &b) {
51     return a.x * b.y - a.y * b.x;
52 }
53 //turn left
54 bool cross(const Point &sp, const Point &ed, const Point &op) {
55     return sgn(across(sp - op, ed - op)) > 0;
56 }
57 
58 /*******************************************************************************************/
59 
60 const int MAXN = 55;
61 
62 Point p[MAXN];
63 bool del[MAXN];
64 int n, T;
65 
66 void solve() {
67     memset(del, 0, sizeof(del));
68     int last = 0;
69     for(int i = 1; i < n; ++i)
70         if(p[i] < p[last]) last = i;
71     for(int i = 0; i < n; ++i) {
72         printf(" %d", last + 1);
73         del[last] = true;
74         int t = 0;
75         for(t = 0; t < n; ++t) if(!del[t]) break;
76         for(int j = 0; j < n; ++j) {
77             if(del[j] || j == t) continue;
78             if(cross(p[j], p[t], p[last])) t = j;
79         }
80         last = t;
81     }
82 }
83 
84 int main() {
85     scanf("%d", &T);
86     while(T--) {
87         scanf("%d", &n);
88         int t;
89         for(int i = 0; i < n; ++i)
90             scanf("%d", &t), p[i].read();
91         printf("%d", n);
92         solve();
93         puts("");
94     }
95 }
View Code

 

posted @ 2013-11-08 17:34  Oyking  阅读(301)  评论(0编辑  收藏  举报