Link: poj 1265
Starting from the origin, we will provide some Dx and Dy moving increments to form a polygon,
Calculate the number of points inside the polygon, the number of points on the edge, and the area.
Additional knowledge:
1. The number of vertices covered by a grid point is gcd (| DX |, | dy |), where, | DX |, | dy | horizontal and vertical increments of line segments.
2,Pick Theorem: Set the number of internal points of the polygon with the grid point as a vertex on the plane to A, the number of points on the edge to B, and the area to S,
Then S = a + B/2-1.
3. the area of any polygon is equal to half of the Cross Product of the vector composed of adjacent two points and the origin point in order.
Idea: Because Dx and Dy in each step are known, use the above knowledge to first obtain the number of points on the edge and the area of the polygon, then the internal point can be obtained.
Note: Do not take the absolute value every time an area is calculated. The cumulative sum and absolute value of the cross product are required.
#include<stdio.h>#include<stdlib.h>int chaji(int x1,int y1,int x2,int y2){ return x1*y2-x2*y1;}int gcd(int a,int b){ return b==0?a:gcd(b,a%b);}int main(){ int T,m,i,j,dx,dy,n,b,x,y; float s; scanf("%d",&T); for(i=1;i<=T;i++){ scanf("%d",&m); scanf("%d%d",&x,&y); b=gcd(abs(x),abs(y)); s=0; for(j=2;j<=m;j++){ scanf("%d%d",&dx,&dy); b+=gcd(abs(dx),abs(dy)); s+=chaji(x,y,x+dx,y+dy); x+=dx; y+=dy; } if(s<0) s=-s; n=(s+2-b)/2; printf("Scenario #%d:\n",i); printf("%d %d %.1f\n\n",n,b,s/2); } return 0;}