http://acm.pku.edu.cn/JudgeOnline/problem?id=2954
给定顶点座标均是整点(或正方形格点)的简单多边形,皮克定理说明了其面积A和内部格点数目i、边上格点数目b的关系:A = i + b/2 - 1.
#include<iostream>
#include<math.h>
#include <stdio.h>
using namespace std;
#define Abs(x) (((x)>0)?(x):(-(x))) /*验证*/
#define MAXN 500
typedef double TYPE;
//空间中的点,可以用来作为二维点来用
struct POINT {/*验证*/
TYPE x; TYPE y; TYPE z;
POINT() : x(0), y(0), z(0) {};
POINT(TYPE _x_, TYPE _y_, TYPE _z_ = 0)
: x(_x_), y(_y_), z(_z_) {};
//要用 G++ 提交 ,可以不用这个
POINT operator =(const POINT &A){
x = A.x;
y = A.y;
z = A.z;
}
};
POINT p[MAXN];
int gcd(int a,int b)
{
if(b==0) return a;
else return gcd(b,a%b);
}
//求多边形面积 只要是多边形就可以求其面积
TYPE Area(int n, POINT p[] ) {
if ( n < 3)
return TYPE(0);
double s = p[0].y * (p[n - 1].x - p[1].x);
for (int i = 1; i < n; i++) {
s += p[i].y * (p[i - 1].x - p[(i + 1) % n].x);
}
return s/2;
}
void PolyPick(int n, POINT p[], int & _I, int & _E, TYPE & _area){
int dx, dy;
_E=0;
for(int i=1; i<=n; i++){
dx=p[i % n].x - p[(i+1)%n].x;
dy=p[i % n].y - p[(i+1)%n].y;
_E+=gcd(Abs(dx),Abs(dy));
}
_area=Area(n,p);
_I=(2*_area+2-_E)/2;
}
int main()
{
int tc,m,i,j,k;
cin>>tc;
TYPE dx,dy;
for(k=1;k<=tc;k++)
{
scanf("%d",&m);
TYPE x=0;
TYPE y=0;
for(i=0;i<m;i++)
{
scanf("%lf%lf",&dx,&dy);
p[i].x=x+dx;
p[i].y=y+dy;
x=p[i].x;
y=p[i].y;
}
int I,E;
double area;
PolyPick(m,p,I, E, area);
printf("Scenario #%d:\n%d %d %.1f\n\n",k,I,E,area);
}
return 0;
}