poj 1696

来源:互联网 发布:淘宝宝贝详情页制作 编辑:程序博客网 时间:2024/05/17 02:09

一道水题,不过还有点意思。。

题目要我们用逆时针的折线尽可能多地把点给连起来,其实在脑海中想象一下情景不难发现如果从当前状态最后的线段中引出一条射线,将其逆时针旋转直到碰到一个点。这需要我们去找到最小的转角,考虑到三角函数中只有余弦函数在0-pi上有单调性,所以用了点积来判断。

而此题并不需要用叉积来判断左拐和右拐,因为我们是从外层包进去的,所以必定是左拐。


#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<queue>#include<cmath>#define inc(i,l,r) for(int i=l;i<=r;i++)#define dec(i,l,r) for(int i=l;i>=r;i--)#define link(x) for(edge *j=h[x];j;j=j->next)#define inf 1e9#define eps 1e-8#define mem(a) memset(a,0,sizeof(a))#define ll long long#define succ(x) (1<<x)#define lowbit(x) (x&&(-x))#define sqr(x) ((x)*(x))#define NM 10000#define nm 100005using namespace std;int read(){int x=0,f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();return f*x;}struct P{int x,y,id;P(int x=0,int y=0,int id=0):x(x),y(y),id(id){}P operator-(const P&o){return P(x-o.x,y-o.y);}int operator*(const P&o){return (x*o.x+y*o.y);}bool operator<(const P&o){return y<o.y||(o.y==y&&x<o.x);}}a[NM],x,y;double dis(P o){return sqrt(sqr(o.x)+sqr(o.y));}bool v[NM];int n,T;int main(){//freopen("data.in","r",stdin);T=read();while(T--){n=read();mem(v);mem(a);printf("%d ",n);inc(i,1,n)a[i].id=read(),a[i].x=read(),a[i].y=read();sort(a+1,a+1+n);printf("%d ",a[1].id);x=P(0,a[1].y);y=a[1];v[1]=true;inc(i,2,n){P t=x;int tmp=0;inc(j,1,n)if(!v[j])if((y-x)*(a[j]-y)*dis(t-y)>(y-x)*(t-y)*dis(a[j]-y))t=a[j],tmp=j;else if((y-x)*(a[j]-y)*dis(t-y)==(y-x)*(t-y)*dis(a[j]-y)&&dis(t-y)>dis(a[j]-y))t=a[j],tmp=j;printf("%d ",t.id);v[tmp]=true;;x=y;y=t;}putchar('\n');}return 0;}

Space AntTime Limit: 1000MSMemory Limit: 10000KTotal Submissions: 4789Accepted: 2986DescriptionThe 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:    It can not turn right due to its special body structure.    It leaves a red path while walking.    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.InputThe 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.OutputOutput 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 Input2101 4 52 9 83 5 94 1 75 3 26 6 37 10 108 8 19 2 410 7 6141 6 112 11 93 8 74 12 85 9 206 3 27 1 68 2 139 15 110 14 1711 13 1912 5 1813 7 314 10 16Sample Output10 8 7 3 4 9 5 6 2 1 1014 9 10 11 5 12 8 7 6 13 4 14 1 3 2SourceTehran 1999[Submit]   [Go Back]   [Status]   [Discuss] 


原创粉丝点击