Scrambled Polygon
A closed polygon is a figure bounded by a finite number of line segments. The intersections of the bounding line segments are called the vertices of the polygon. When one starts at any vertex of a closed polygon and traverses each bounding line segment exactly once, one comes back to the starting vertex.
A closed polygon is called convex if the line segment joining any two points of the polygon lies in the polygon. Figure 1 shows a closed polygon which is convex and one which is not convex. (Informally, a closed polygon is convex if its border doesn't have any "dents".)
The subject of this problem is a closed convex polygon in the coordinate plane, one of whose vertices is the origin (x = 0, y = 0). Figure 2 shows an example. Such a polygon will have two properties significant for this problem.
The first property is that the vertices of the polygon will be confined to three or fewer of the four quadrants of the coordinate plane. In the example shown in Figure 2, none of the vertices are in the second quadrant (where x < 0, y > 0).
To describe the second property, suppose you "take a trip" around the polygon: start at (0, 0), visit all other vertices exactly once, and arrive at (0, 0). As you visit each vertex (other than (0, 0)), draw the diagonal that connects the current vertex with (0, 0), and calculate the slope of this diagonal. Then, within each quadrant, the slopes of these diagonals will form a decreasing or increasing sequence of numbers, i.e., they will be sorted. Figure 3 illustrates this point.
Input
The input lists the vertices of a closed convex polygon in the plane. The number of lines in the input will be at least three but no more than 50. Each line contains the x and y coordinates of one vertex. Each x and y coordinate is an integer in the range -999..999. The vertex on the first line of the input file will be the origin, i.e., x = 0 and y = 0. Otherwise, the vertices may be in a scrambled order. Except for the origin, no vertex will be on the x-axis or the y-axis. No three vertices are colinear.
Output
The output lists the vertices of the given polygon, one vertex per line. Each vertex from the input appears exactly once in the output. The origin (0,0) is the vertex on the first line of the output. The order of vertices in the output will determine a trip taken along the polygon's border, in the counterclockwise direction. The output format for each vertex is (x,y) as shown below.
Sample Input
0 0
70 -50
60 30
-30 -50
80 20
50 -60
90 -20
-30 -40
-10 -60
90 10
Sample Output
(0,0)
(-30,-40)
(-30,-50)
(-10,-60)
(50,-60)
(70,-50)
(90,-20)
(90,10)
(80,20)
(60,30)
Source
题目类型:求凸包+输出方案
算法分析:使用Garham算法求解即可,注意栈中的方案是顺时针序的,需要进行一个简单转换
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
/************************************************* Author :supermaker Created Time :2016/1/26 21:11:32 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 66; int sgn (double val) { if (fabs (val) < EPS) return 0; else if (val < 0) return -1; return 1; } struct Point { double x, y; Point () {} Point (double xx, double yy) : x (xx), y (yy) {} Point operator + (const Point &a) const { return Point (x + a.x, y + a.y); } Point operator - (const Point &a) const { return Point (x - a.x, y - a.y); } double operator ^ (const Point &a) const { return x * a.y - y * a.x; } double operator * (const Point &a) const { return x * a.x + y * a.y; } }; Point p[maxn]; int n; stack <int> sta; vector <Point> res; double Dist (Point p1, Point p2) { return sqrt ((p2 - p1) * (p2 - p1)); } bool polarcmp (Point p1, Point p2) { double tt = (p1 - p[1]) ^ (p2 - p[1]); if (sgn (tt) == 1) return true; else if (sgn (tt) == 0 && sgn (Dist (p1, p[1]) - Dist (p2, p[1])) <= 0) return true; return false; } void Garham () { Point start = p[1]; int start_pos = 1; for (int i = 1; i <= n; i++) if (sgn (p[i].x - start.x) == -1 || (sgn (p[i].x - start.x) == 0 && sgn (p[i].y - start.y) == -1)) start = p[i], start_pos = i; swap (p[start_pos], p[1]); sort (p + 2, p + n + 1, polarcmp); while (!sta.empty ()) sta.pop (); sta.push (1), sta.push (2); for (int i = 3; i <= n; i++) { while (sta.size () >= 2) { int tt1 = sta.top (); sta.pop (); int tt2 = sta.top (); sta.pop (); if (sgn ((p[tt1] - p[tt2]) ^ (p[i] - p[tt2])) <= 0) sta.push (tt2); else { sta.push (tt2), sta.push (tt1); break; } } sta.push (i); } } int main() { //CFF; //CPPFF; n = 0; double sx, sy; while (scanf ("%lf%lf", &sx, &sy) != EOF) { p[++n] = Point (sx, sy); } Garham (); res.clear (); while (!sta.empty ()) { int tt = sta.top (); sta.pop (); res.push_back (p[tt]); } vector <Point> ans; set <int> seta; bool is_have = false; for (int i = res.size () - 1; i >= 0; i--) { if (sgn (res[i].x) == 0 && sgn (res[i].y) == 0) ans.push_back (res[i]), is_have = true, seta.insert (i); else if (is_have) ans.push_back (res[i]), seta.insert (i); } for (int i = res.size () - 1; i >= 0; i--) if (seta.find (i) == seta.end ()) ans.push_back (res[i]), seta.insert (i); for (int i = 0; i < ans.size (); i++) printf ("(%.lf,%.lf)\n", ans[i].x, ans[i].y); return 0; } |
- poj1873:下一篇 »