UVa 10088 - Trees on My Island

题目:在整数构成的二维平面上,统计多边形内整数坐标点的个数。

分析:计算几何、pick定理。根据pick定理,我们有在坐标为整数的二维平面内,有s=a+b/2-1,其中b是落在边上的点数,a是内部点数。由此可知:a=s-b/2+1,其中s可利用叉乘求解;b利用gcd求解即可。

注意:面积要取绝对值。

#include <iostream>
#include <cstdlib>
#include <cstdio>

using namespace std;

typedef long long LL;

typedef struct pnode{
	LL x,y;
}point;
point P[1005];

LL  ab( LL v )
{
	if ( v >= 0LL ) return v;
	else return 0LL-v;
}

LL  gcd( LL a, LL b )
{
	if ( b == 0LL ) return a;
	return a%b?gcd(b,a%b):b;
}

LL  crossproduct( point a, point b, point c )
{
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y)+0LL;
}

int main()
{
	int N;
	while ( cin >> N && N ) {
		for ( int i = 0 ; i < N ; ++ i )
			cin >> P[i].x >> P[i].y;
		P[N] = P[0];
		
		LL a,b,r,count = 0LL;
		for ( int i = 0 ; i < N ; ++ i ) {
			a = ab( P[i+1].x - P[i].x );
			b = ab( P[i+1].y - P[i].y );
			r = gcd( a, b );
			count += r + 0LL;
		}
		
		LL area = 0LL;
		for ( int i = 2 ; i < N ; ++ i )
			area += crossproduct( P[0], P[i-1], P[i] );
		
		cout << ab(area)/2LL+1LL-count/2LL << endl;
	}
	return 0;
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值