首页 > 代码库 > uva 10065 (凸包+求面积)
uva 10065 (凸包+求面积)
链接: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 (x, y) 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
50 0
2 0
2 2
1 1
0 2
5
0 0
0 2
1 3
2 2
2 0
0
Sample Output
Tile #1Wasted 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 }
第一道uva的题,做个纪念、uva 的邮件
××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
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.
The UVa Online Judge team