Pick定理是说,在一个平面直角坐标系内,如果一个多边形的顶点全都在格点上,那么这个图形的面积恰好就等于边界上经过的格点数的一半加上内部所含格点数再减一。
题意不好懂,给出的x,y并不是坐标而是向x轴方向y轴方向移动的距离。
1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue> 9 #include<set> 10 using namespace std; 11 #define N 110 12 #define LL long long 13 #define INF 0xfffffff 14 const double eps = 1e-8; 15 const double pi = acos(-1.0); 16 const double inf = ~0u>>2; 17 struct Point 18 { 19 double x,y; 20 Point(double x=0,double y=0):x(x),y(y) {} 21 }p[N]; 22 typedef Point pointt; 23 pointt operator + (Point a,Point b) 24 { 25 return Point(a.x+b.x,a.y+b.y); 26 } 27 pointt operator - (Point a,Point b) 28 { 29 return Point(a.x-b.x,a.y-b.y); 30 } 31 int dcmp(double x) 32 { 33 if(fabs(x)<eps) return 0; 34 else return x<0?-1:1; 35 } 36 double cross(Point a,Point b) 37 { 38 return a.x*b.y-a.y*b.x; 39 } 40 double Polyarea(int n) 41 { 42 double area = 0; 43 for(int i = 1 ; i < n-1 ; i++) 44 area+=cross(p[i]-p[0],p[i+1]-p[0]); 45 return area/2; 46 } 47 int main() 48 { 49 int t,i,n,kk=0; 50 cin>>t; 51 while(t--) 52 { 53 scanf("%d",&n); 54 int num = 0; 55 p[0].x = 0,p[0].y = 0; 56 for(i = 1; i <= n ;i++) 57 { 58 scanf("%lf%lf",&p[i].x,&p[i].y); 59 p[i].x+=p[i-1].x; 60 p[i].y+=p[i-1].y; 61 } 62 p[n+1] = 0; 63 for(i = 0; i <= n ;i++) 64 { 65 Point pp = p[i]-p[i+1]; 66 pp.x = fabs(pp.x),pp.y = fabs(pp.y); 67 if(dcmp(pp.x)==0||dcmp(pp.y)==0) 68 num+=pp.x+pp.y; 69 else 70 num+=__gcd((int)pp.x,(int)pp.y); 71 } 72 double s = Polyarea(n); 73 printf("Scenario #%d: ",++kk); 74 printf("%d %d %.1f ",(int)s+1-num/2,num,s); 75 puts(""); 76 } 77 return 0; 78 }