题目连接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=43009#problem/W
题目意思:
给三角形的三个坐标(均为整形数),求在三角形内的整数点的个数。
题目想法:
起初一看到题目就想到暴力,直接求解,可是,钻进了解析几何的思维,但是,注意的是,计算几何呀。会存在精度丢失。
pick定理:s = n + e/2 -1 (面积、内部点数、线段点数)
线段上的点数,包括2个断点, e = gcd ( |x1-x2| , |y1-y2| );
#include<iostream>
#include<cstdio>
using namespace std;
struct node
{
int x,y;
}P[3];
struct line
{
node a,b;
}l[3];
int xmult(node a,node b,node c)
{
int x1,y1,x2,y2;
x1=b.x-a.x; y1=b.y-a.y;
x2=c.x-a.x; y2=c.y-a.y;
return x1*y2-x2*y1;
}
int gcd(int a,int b)
{
if(b==0)return a;
return gcd(b,a%b);
}
int Abs(int a){return a>0?a:-a;}
int e_count(line l)
{
return gcd(Abs(l.a.x-l.b.x),Abs(l.a.y-l.b.y));
}
int main()
{
//freopen("w.txt","r",stdin);
while(scanf("%d%d%d%d%d%d",&P[0].x,&P[0].y,&P[1].x,&P[1].y,&P[2].x,&P[2].y))
{//s=n+e/2-1;
if(!(P[0].x||P[0].y||P[1].x||P[1].y||P[2].x||P[2].y))break;
l[0].a=P[0];l[0].b=P[1];
l[1].a=P[1];l[1].b=P[2];
l[2].a=P[2];l[2].b=P[0];
int s=xmult(P[0],P[1],P[2])/2;
int e=e_count(l[0])+e_count(l[1])+e_count(l[2]);//线段上的点为|x|和|y|的e_count+1
int n=Abs(s)+1-e/2;
printf("%d\n",n);
}
return 0;
}