hust训练-计算几何之 pick定理

题目连接:

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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值