PICK 公式: S=e/2+i-1 S为多边形面积 i 为多边形内部的格点数,e为多边形边上的格点数

http://acm.pku.edu.cn/JudgeOnline/problem?id=2954

#include <stdio.h>
#include
<string.h>
#include
<iostream>
using namespace std;
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;
}
};
double gcd(int x,int y)
{
if (y==0) return x;
return gcd(y,x%y);
}
inline
int ABS(int x)
{
return x>0? x:-x;
}
int TrianglePoint(const POINT *p)
{
int x1,x2,x3,y1,y2,y3,AA,e,ans;//AA means double area

x1
= (int)p[0].x; y1 = (int)p[0].y;
x2
= (int)p[1].x; y2 = (int)p[1].y;
x3
= (int)p[2].x; y3 = (int)p[2].y;
int dx12=ABS(x1-x2);
int dx13=ABS(x1-x3);
int dx23=ABS(x2-x3);
int dy12=ABS(y1-y2);
int dy13=ABS(y1-y3);
int dy23=ABS(y2-y3);
AA
=ABS((x1-x2)*(y2-y3)-(x2-x3)*(y1-y2));
e
=gcd(dx12,dy12)+gcd(dx13,dy13)+gcd(dx23,dy23);
ans
=(AA+2-e)/2;
return ans;

}
int main(){
POINT p[
3];
while (cin>>p[0].x>>p[0].y>>p[1].x>>p[1].y>>p[2].x>>p[2].y){
//刚开始写成 a+b+c+d+e+f==0 的形式,一直 WA,原因是有负数
if( p[0].x == 0 && p[0].y == 0 && p[1].x == 0 && p[1].y== 0 && p[2].x==0 &&p[2].y == 0)
break;
int ans=TrianglePoint(p);
cout
<<ans<<endl;
}
return 0;
}

posted on 2011-05-09 22:06  敌敌  阅读(271)  评论(0编辑  收藏  举报