POJ 1265 Area (pick定理)

題目大意:已知機器人行走步數及每一步的座標變化量,求機器人所走路徑圍成的多邊形的面積、多邊形邊上和內部的點的數量。

思路:叉積求面積,pick定理求點。

pick定理:面積=內部點數+邊上點數/2-1。

// Time 0ms; Memory 236K
#include<iostream>
#include<cstdio>
#include<cmath>

using namespace std;

struct point 
{
	int x,y;
	point(int xx=0,int yy=0):x(xx),y(yy){}
}a,b;
int gcd(int x,int y)
{
	static int t;
	for(;t=y;y=x%y,x=t);
	return x;
}
int main()
{
	int i,j,t,m,p,q,A,I,E;
	cin>>t;
	for(i=0;i<t;i++)
	{
		A=E=0;
		cin>>m;
		for(j=0;j<m;j++)
		{
			cin>>p>>q;
			a=b;
			b=point(a.x+p,a.y+q);
			A+=a.x*b.y-a.y*b.x;
			E+=abs(gcd(a.x-b.x,a.y-b.y));
		}
		A=abs(A);
		I=(A-E)/2+1; //pick定理
		printf("Scenario #%d:\n",i+1);
		printf("%d %d %.1lf\n\n",I,E,A*0.5);
	}
	return 0;
}

發佈了116 篇原創文章 · 獲贊 217 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章