uva 10065 (凸包+求面積)

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   os   io   strong   

連結:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=&problem=1006

Problem A

Useless Tile Packers

Input: standard input

Output: standard output

 

Yes, as you have apprehended the Useless Tile Packers (UTP) pack tiles. The tiles are of uniform thickness and have simple polygonal shape. For each tile a container is custom-built. The floor of the container is a convex polygon and under this constraint it has the minimum possible space inside to hold the tile it is built for. But this strategy leads to wasted space inside the container.

 

                             

 

The UTP authorities are interested to know the percentage of wasted space for a given tile.

 

Input

The input file consists of several data blocks. Each data block describes one tile.

The first line of a data block contains an integer N (3 <= N <= 100) indicating the number of corner points of the tile. Each of the next N lines contains two integers giving the (xy) co-ordinates of a corner point (determined using a suitable origin and orientation of the axes) where 0 <= x, , y <= 1000. Starting from the first point given in the input the corner points occur in the same order on the boundary of the tile as they appear in the input. No three consecutive points are co-linear.

The input file terminates with a value of 0 for N.

 

Output

For each tile in the input output the percentage of wasted space rounded to two digits after the decimal point. Each output must be on a separate line. Print a blank line after each output block.

 

 

Sample Input

5
0 0
2 0
2 2
1 1
0 2
5
0 0
0 2
1 3
2 2
2 0
0

 

Sample Output

Tile #1
Wasted Space = 25.00 %

Tile #2
Wasted Space = 0.00 %

________________________________________________________________________________
Rezaul Alam Chowdhury

 

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

讓求閒置面積,簡單轉化一下,就是求建造凸包之前的面積,以及建造凸包之後的面積,相減,再相除即可

犯了個好傻的毛病,把求叉乘積的函數,以及距離的函數類型定義為bool,搞的一直出不來結果,最後調試兩遍才發現,汗!!!

 

 1 #include <stdio.h> 2 #include <math.h> 3 #include <string.h> 4 #include <stdlib.h> 5 #include <iostream> 6 #include <algorithm> 7  8 using namespace std; 9 #define MAXX 10510 #define eps 1e-811 12 typedef struct point13 {14     double x;15     double y;16 }point;17 18 bool xy(double x,double y){ return x<y-eps; }19 bool dy(double x,double y){ return x>y+eps; }20 bool xyd(double x,double y){ return x<y+eps; }21 bool dyd(double x,double y){ return x>y-eps; }22 bool dd(double x,double y){ return fabs(x-y)<eps; }23 24 double crossProduct(point a,point b,point c)25 {26     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);27 }28 double dist(point a,point b)29 {30     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));31 }32 33 point p[MAXX];34 point stk[MAXX];35 int top;36 37 bool cmp(point a,point b)38 {39     double len=crossProduct(p[0],a,b);40     if(dd(len,0.0))41     {42         return xy(dist(p[0],a),dist(p[0],b));43     }44     return xy(len,0.0);45 }46 47 void Graham(int n)48 {49     int tmp=0;50     for(int i=1; i<n; i++)51     {52         if(xy(p[i].x,p[tmp].x) || dd(p[i].x,p[tmp].x) && xy(p[i].y,p[tmp].y))53         {54             tmp=i;55         }56     }57     swap(p[0],p[tmp]);58     sort(p+1,p+n, cmp);59 60     stk[0]=p[0];61     stk[1]=p[1];62     top=1;63     for(int i=2; i<n; i++)64     {65         while(top && xyd(crossProduct(stk[top],stk[top-1],p[i]),0.0))66             top--;67         stk[++top]=p[i];68     }69 }70 71 double Area(int n,point ss[])72 {73     double ans=0.0;74     for(int i=2; i<n; i++)75     {76         ans+=crossProduct(ss[0],ss[i-1],ss[i]);//printf("%lf^^\n",ans);77     }78     return fabs(ans)/2.0;79 }80 81 int main()82 {83     int n,m,i,j;84     int cas=1;85     while(scanf("%d",&n)!=EOF &&n)86     {87         for(i=0; i<n; i++)88             scanf("%lf%lf",&p[i].x,&p[i].y);89         double ans_min=Area(n,p);90         Graham(n);//printf("%d--",top);91         double ans_max=Area(top+1,stk);92         printf("Tile #%d\n",cas++);93         printf("Wasted Space = %.2lf %%\n\n",(ans_max-ans_min)/ans_max*100);94     }95     return 0;96 }
View Code

 

第一道uva的題,做個紀念、uva 的郵件

××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

Hi,

This is an automated response from UVa Online Judge.

Your submission with number  14050702 for the problem  10065 - Useless Tile Packers has succeeded with verdict  Accepted.

Congratulations! Now it is time to try a new problem.

Best regards,

The UVa Online Judge team×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.