您的位置:首页 > 其它

POJ 1696 Space Ant 【极角排序】

2012-03-24 23:56 561 查看
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:

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.
View Code

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
struct P
{
int x,y;
int xu;
}p[103],tmp;
int mul(P p1,P p2,P p3)
{
return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int dis(P a,P b)
{
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
int cmp(const void*a,const void*b)
{
P* c=(P*)a;
P* d=(P*)b;
int k=mul(tmp,*c,*d);
if(k>0)
return -1;
if(k<0)
return 1;
else if(k==0)
return dis(tmp,*c)-dis(tmp,*d);
}
int a[102];
int main()
{
int i,t,n,st,xx,yy,top;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
xx=2000;  yy=2000;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&p[i].xu,&p[i].x,&p[i].y);
if(p[i].y<yy){  xx=p[i].x;  yy=p[i].y;  st=i;}
else if(p[i].y==yy&&p[i].x<xx)
{ xx=p[i].x;  yy=p[i].y;  st=i;   }
}
tmp=p[st];  p[st]=p[0];  p[0]=tmp;
top=-1;
qsort(p+1,n-1,sizeof(p[0]),cmp);
a[++top]=p[0].xu;
for(i=1;i<n-1;i++)
{
tmp=p[i];
qsort(p+i+1,n-i-1,sizeof(p[0]),cmp);
a[++top]=p[i].xu;
}
a[++top]=p[n-1].xu;
printf("%d ",top+1);
for(i=0;i<=top;i++)
printf("%d%c",a[i],i==top?'\n':' ');
}
return 0;
}



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: