zju 1032 皮克定理
这个题目要用到两点知识:
1)计算多边形的面积,这个是通过将多边形分割成三角形,然后用叉积来计算三角形的面积(有正负之分)
2)格点多边形的面积A于多边形边上的格点数B,多边形内部的格点数I之间的关系为
A=I+B/2-1
即Pick's theorem,详见 http://en.wikipedia.org/wiki/Pick%27s_theorem
code:
1)计算多边形的面积,这个是通过将多边形分割成三角形,然后用叉积来计算三角形的面积(有正负之分)
2)格点多边形的面积A于多边形边上的格点数B,多边形内部的格点数I之间的关系为
A=I+B/2-1
即Pick's theorem,详见 http://en.wikipedia.org/wiki/Pick%27s_theorem
code:
#include <stdio.h>
#define MAXN 100
struct point{ double x,y;};
double xmult(double x1,double y1,double x2,double y2){ return (x1*y2-y1*x2);}
int gcd(int a,int b) { return b?gcd(b,a%b):a;}
int abs(int x) { return x>0?x:(0-x);}
double area_polygon(int n, point *p)
{
int i;
double ret=0;
for (i=0; i<n; ++i)
ret += xmult(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
ret += xmult(p[n-1].x,p[n-1].y,p[0].x,p[0].y);
return ret/2;
}
void solve()
{
int i,n;
point p[MAXN+1];
int c,e;
double a;
int dx,dy;
scanf ("%d",&n);
p[0].x = p[0].y = 0;
e = 0;
for (i=0; i<n; ++i){
scanf ("%d %d",&dx,&dy);
e += gcd(abs(dx),abs(dy));
p[i+1].x = p[i].x+dx;
p[i+1].y = p[i].y+dy;
}
a = area_polygon(n+1,p);
c = (int)(a+1-0.5*(double)e);
printf ("%d %d %.1lf\n\n",c,e,a);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1032.txt","r",stdin);
#endif
int i,t;
while (scanf ("%d",&t) != EOF)
for (i=1; i<=t; ++i){
printf ("Scenario #%d:\n",i);
solve();
}
#ifdef ONLINE_JUDGE
#else
fclose(stdin);
#endif
return 0;
}
#define MAXN 100
struct point{ double x,y;};
double xmult(double x1,double y1,double x2,double y2){ return (x1*y2-y1*x2);}
int gcd(int a,int b) { return b?gcd(b,a%b):a;}
int abs(int x) { return x>0?x:(0-x);}
double area_polygon(int n, point *p)
{
int i;
double ret=0;
for (i=0; i<n; ++i)
ret += xmult(p[i].x,p[i].y,p[i+1].x,p[i+1].y);
ret += xmult(p[n-1].x,p[n-1].y,p[0].x,p[0].y);
return ret/2;
}
void solve()
{
int i,n;
point p[MAXN+1];
int c,e;
double a;
int dx,dy;
scanf ("%d",&n);
p[0].x = p[0].y = 0;
e = 0;
for (i=0; i<n; ++i){
scanf ("%d %d",&dx,&dy);
e += gcd(abs(dx),abs(dy));
p[i+1].x = p[i].x+dx;
p[i+1].y = p[i].y+dy;
}
a = area_polygon(n+1,p);
c = (int)(a+1-0.5*(double)e);
printf ("%d %d %.1lf\n\n",c,e,a);
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("1032.txt","r",stdin);
#endif
int i,t;
while (scanf ("%d",&t) != EOF)
for (i=1; i<=t; ++i){
printf ("Scenario #%d:\n",i);
solve();
}
#ifdef ONLINE_JUDGE
#else
fclose(stdin);
#endif
return 0;
}