0
点赞
收藏
分享

微信扫一扫

【POJ - 1696】Space Ant (凸包,最小极角,排序)

sin信仰 2022-06-15 阅读 21

题干:

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.

Sample Input

2
10
1 4 5
2 9 8
3 5 9
4 1 7
5 3 2
6 6 3
7 10 10
8 8 1
9 2 4
10 7 6
14
1 6 11
2 11 9
3 8 7
4 12 8
5 9 20
6 3 2
7 1 6
8 2 13
9 15 1
10 14 17
11 13 19
12 5 18
13 7 3
14 10 16

Sample Output

10 8 7 3 4 9 5 6 2 1 10
14 9 10 11 5 12 8 7 6 13 4 14 1 3 2

题目大意:

    一只蚂蚁,只会向左转,现在给出平面上很多个点,求解一种走法,能使得蚂蚁能经过的点最多,每个顶点该蚂蚁只能经过一次,且所行走的路线不能发生交叉(或者说,问最多能走多少个点)

解题报告:

    求个凸包就好了。注意这种边排序边选择的方法。

    至于cmp函数为什么可以这么写,也很好证明,因为题干中说了行走的路线不能交叉,

AC代码:

#include<iostream>
#include<algorithm>
#include<queue>
#include<cstdio>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const double eps = 1e-8;
int sgn(double x) {
if(fabs(x) < eps)return 0;
if(x < 0) return -1;
return 1;
}
struct Point {
double x,y;
int id;
Point(){}
Point(double x,double y):x(x),y(y){}
Point operator -(const Point &b)const {
return Point(x - b.x,y - b.y);
}
double operator ^(const Point &b)const {
return x*b.y - y*b.x;
}
double operator *(const Point &b)const {
return x*b.x + y*b.y;
}
} p[1005];
int pos,n;
struct Line {
Point s,e;
Line(){}
Line(Point s,Point e):s(s),e(e){}
pair<Point,int> operator &(const Line &b)const {
Point res = s;
if(sgn((s-e)^(b.s-b.e)) == 0)
{
if(sgn((b.s-s)^(b.e-s)) == 0)
return make_pair(res,0);//两直线重合
else return make_pair(res,1);//两直线平行
}
double t = ((s-b.s)^(b.s-b.e))/((s-e)^(b.s-b.e));
res.x += (e.x - s.x)*t;
res.y += (e.y - s.y)*t;
return make_pair(res,2);//有交点
}
};
inline double xmult(Point p0,Point p1,Point p2) { //p0p1 X p0p2
return (p1-p0)^(p2-p0);
}
bool Seg_inter_line(Line l1,Line l2) { //判断直线l1和线段l2是否相交
return sgn(xmult(l2.s,l1.s,l1.e))*sgn(xmult(l2.e,l1.s,l1.e)) <= 0;
}
double dist(Point a,Point b) {
return sqrt((b - a)*(b - a));
}
bool cmp(const Point & a,const Point & b) {
double tmp = xmult(p[pos],a,b);
if(sgn(tmp) < 0) return 0;
else if(sgn(tmp) > 0) return 1;
else return dist(a,p[pos]) < dist(b,p[pos]);
}
int main()
{
int t;
cin>>t;
double x1,x2,x3,x4,y1,y2,y3,y4;
while(t--) {
scanf("%d",&n);
for(int i = 1; i<=n; i++) {
scanf("%d%lf%lf",&p[i].id,&p[i].x,&p[i].y);
}
double curx=1000000,cury=1000000;
int curid = 1;
for(int i = 1; i<=n; i++) {
if(p[i].y < cury || (p[i].y==cury&&p[i].x < curx)) {
cury = p[i].y;curx = p[i].x;curid = i;
}
}
swap(p[1],p[curid]);
pos = 1;
for(int i = 2; i<=n; i++) {
sort(p+i,p+n+1,cmp);
pos++;
}
printf("%d",n);
for(int i = 1; i<=n; i++) {
printf(" %d",p[i].id);
}
puts("");
}
return 0 ;
}

总结:两种问法虽然是等价的,会让你的思路方向就不同了,,,第一种很容易让你想到正解,但是第二种就不让你往这上面想。


举报

相关推荐

0 条评论