POJ 1969 Space Ant

 蒻苣:弱弱很弱,路过的巨巨还不吝赐教!^.^...QAQ

题意:一只蚂蚁,n个点,蚂蚁从y最小的地方开始爬,爬到最后。

爬的方式:

1,不能右转

2,不能与走过的路线交叉

题解:做法有很多,觉得代码量少的是极角排序,即每次找左边倾斜最小的点,如果倾斜为0,则选近的。

代码:

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <cmath>
 8 using namespace std;
 9 const double INF  = 1E200;
10 const double wc = 1E-8;
11 struct POINT{
12         double x,y;
13         int id,val;
14         POINT( double a = 0,double b = 0 )  { x = a; y = b; }
15 };
16 POINT p[100+5];
17 bool cmp( POINT a, POINT b )
18 {
19         return a.val<b.val;
20 }
21 struct LINESEG{
22         POINT e;
23         POINT s;
24         LINESEG( POINT x,POINT y ) { e = x; s = y; }
25         LINESEG(){}
26 };
27 double dist(POINT p1,POINT p2)
28 {
29  return( sqrt( (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y) ) );
30 }
31 double multiply(POINT sp,POINT ep,POINT op)
32 {
33  return((sp.x-op.x)*(ep.y-op.y)-(ep.x-op.x)*(sp.y-op.y));
34 }
35 int n;
36 void dfs(int st, int dep)
37 {
38       if(dep == n) return;
39       int mi = 0;
40       while(p[mi].val!=-1) mi++;
41       for(int i = mi+1;i<n;i++)
42       {
43               if(p[i].val!=-1) continue;
44               int r = multiply(p[st],p[i],p[mi]);
45               if(r>wc) mi = i;
46               else if(abs(r) < wc && dist(p[st], p[i]) < dist(p[st], p[mi])) mi = i;
47       }
48       p[mi].val =dep;
49       dfs(mi,dep+1);
50 }
51 int main()
52 {
53        int test;
54        cin>>test;
55         while(test--)
56         {       cin>>n;
57                 int iid=0;
58                 for(int i = 0; i < n; i++)
59                 {
60                         cin>>p[i].id>>p[i].x>>p[i].y;
61                         if(p[i].y<p[iid].y) iid = i;
62                         p[i].val = -1;
63                 }
64                 p[iid].val = 0;
65                 dfs(iid,1);
66                 sort(p,p+n,cmp);
67                 cout<<n;
68                 for(int i = 0;i < n;i++)
69                         cout<<" "<<p[i].id;
70                 cout<<endl;
71         }
72 }
View Code

 

posted on 2015-08-21 23:29  小松song  阅读(91)  评论(0)    收藏  举报

导航