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 (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,搞的一直出不来结果,最后调试两遍才发现,汗!!!

 #include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <algorithm> using namespace std;
#define MAXX 105
#define eps 1e-8 typedef struct point
{
double x;
double y;
}point; bool xy(double x,double y){ return x<y-eps; }
bool dy(double x,double y){ return x>y+eps; }
bool xyd(double x,double y){ return x<y+eps; }
bool dyd(double x,double y){ return x>y-eps; }
bool dd(double x,double y){ return fabs(x-y)<eps; } double crossProduct(point a,point b,point c)
{
return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
}
double dist(point a,point b)
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} point p[MAXX];
point stk[MAXX];
int top; bool cmp(point a,point b)
{
double len=crossProduct(p[],a,b);
if(dd(len,0.0))
{
return xy(dist(p[],a),dist(p[],b));
}
return xy(len,0.0);
} void Graham(int n)
{
int tmp=;
for(int i=; i<n; i++)
{
if(xy(p[i].x,p[tmp].x) || dd(p[i].x,p[tmp].x) && xy(p[i].y,p[tmp].y))
{
tmp=i;
}
}
swap(p[],p[tmp]);
sort(p+,p+n, cmp); stk[]=p[];
stk[]=p[];
top=;
for(int i=; i<n; i++)
{
while(top && xyd(crossProduct(stk[top],stk[top-],p[i]),0.0))
top--;
stk[++top]=p[i];
}
} double Area(int n,point ss[])
{
double ans=0.0;
for(int i=; i<n; i++)
{
ans+=crossProduct(ss[],ss[i-],ss[i]);//printf("%lf^^\n",ans);
}
return fabs(ans)/2.0;
} int main()
{
int n,m,i,j;
int cas=;
while(scanf("%d",&n)!=EOF &&n)
{
for(i=; i<n; i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
double ans_min=Area(n,p);
Graham(n);//printf("%d--",top);
double ans_max=Area(top+,stk);
printf("Tile #%d\n",cas++);
printf("Wasted Space = %.2lf %%\n\n",(ans_max-ans_min)/ans_max*);
}
return ;
}

第一道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

×××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××××
上一篇:ubuntu 14.04 hadoop eclipse 0配置基本环境


下一篇:linux下怎么编译运行C语言程序?