Shirlies
宁静专注认真的程序媛~

晕死,一个循环害死我了,改了好长时间一直WA,搞得我莫名其妙,真是百思不得其解,现在才发现时一个while写成了if,晕死~~~

代码如下:
 1 #include <cstdio>
 2 #include <cmath>
 3 #include <algorithm>
 4 using namespace std;
 5 
 6 const double pi = acos(-1.0);
 7 struct node
 8 {
 9     int x,y;
10 }p[1100],stack[1100];
11 
12 int n;
13 double length;
14 int top;
15 
16 double dis(node a,node b)
17 {
18     return sqrt(1.0 * (a.x - b.x) * (a.x - b.x) + 1.0 * (a.y - b.y) * (a.y - b.y));
19 }
20 
21 double cross_product(node a,node b,node o)
22 {
23     return 1.0*(a.x - o.x) * (b.y - o.y) - 1.0*(b.x - o.x) * (a.y - o.y);
24 }
25 
26 bool cmp(node a,node b)
27 {
28     double temp = cross_product(a,b,p[0]);
29     if(temp == 0)
30     {
31         return dis(a,p[0]) < dis(b,p[0]);
32     }
33 
34     return temp > 0;
35 }
36 
37 void convex_hull()
38 {
39     stack[0] = p[0];
40     stack[1] = p[1];
41     top = 1;
42     for(int i = 2;i < n;i ++)
43     {
44         while(top&&cross_product(p[i],stack[top],stack[top - 1]) >= 0)//就是这个while,我竟然写成了if!!!!
45         {
46             top --;
47         }
48         stack[++top] = p[i];
49     }
50 }
51 
52 int main()
53 {
54     int cas;
55     scanf("%d",&cas);
56     while(cas --)
57     {
58         scanf("%d%lf",&n,&length);
59         for(int i = 0;i < n;i ++)
60         {
61             scanf("%d%d",&p[i].x,&p[i].y);
62             if(i)
63             {
64                 if(p[i].y < p[0].y||(p[i].y == p[0].y && p[i].x < p[0].x))
65                 {
66                     swap(p[i],p[0]);
67                 }
68             }
69         }
70 
71         sort(p+1,p+n,cmp);
72 
73         convex_hull();
74 
75         double ans = 0;
76         for(int i = 0;i < top;i ++)
77         {
78             ans += dis(stack[i],stack[i+1]);
79         }
80 
81         ans += dis(stack[0],stack[top]);
82         ans += 2.0*pi * length;
83 
84         printf("%.lf\n",ans);
85         if(cas)
86             printf("\n");
87     }
88 
89     return 0;
90 }

 

posted on 2012-05-22 21:38  Shirlies  阅读(311)  评论(0编辑  收藏  举报