首页 > 其他 > 详细

POJ 1265 pick定理

时间:2014-04-22 14:58:46      阅读:525      评论:0      收藏:0      [点我收藏+]
Area
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 4637   Accepted: 2099

Description

Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new research and development facility the company has installed the latest system of surveillance robots patrolling the area. These robots move along the walls of the facility and report suspicious observations to the central security office. The only flaw in the system a competitor抯 agent could find is the fact that the robots radio their movements unencrypted. Not being able to find out more, the agent wants to use that information to calculate the exact size of the area occupied by the new facility. It is public knowledge that all the corners of the building are situated on a rectangular grid and that only straight walls are used. Figure 1 shows the course of a robot around an example area.

bubuko.com,布布扣
Figure 1: Example area.

You are hired to write a program that calculates the area occupied by the new facility from the movements of a robot along its walls. You can assume that this area is a polygon with corners on a rectangular grid. However, your boss insists that you use a formula he is so proud to have found somewhere. The formula relates the number I of grid points inside the polygon, the number E of grid points on the edges, and the total area A of the polygon. Unfortunately, you have lost the sheet on which he had written down that simple formula for you, so your first task is to find the formula yourself.

Input

The first line contains the number of scenarios.
For each scenario, you are given the number m, 3 <= m < 100, of movements of the robot in the first line. The following m lines contain pairs 揹x dy?of integers, separated by a single blank, satisfying .-100 <= dx, dy <= 100 and (dx, dy) != (0, 0). Such a pair means that the robot moves on to a grid point dx units to the right and dy units upwards on the grid (with respect to the current position). You can assume that the curve along which the robot moves is closed and that it does not intersect or even touch itself except for the start and end points. The robot moves anti-clockwise around the building, so the area to be calculated lies to the left of the curve. It is known in advance that the whole polygon would fit into a square on the grid with a side length of 100 units.

Output

The output for every scenario begins with a line containing 揝cenario #i:? where i is the number of the scenario starting at 1. Then print a single line containing I, E, and A, the area A rounded to one digit after the decimal point. Separate the three numbers by two single blanks. Terminate the output for the scenario with a blank line.

Sample Input

2
4
1 0
0 1
-1 0
0 -1
7
5 0
1 3
-2 2
-1 0
0 -3
-3 1
0 -3

Sample Output

Scenario #1:
0 4 1.0

Scenario #2:
12 16 19.0


复制一段讲解,慢慢理解。

S=a+ b/2 - 1

  (其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积)

其实这个公式主要是用来求a的,s可由叉积得出,b可由gcd求出。

pick公式无比牛逼证明(感谢ldl提供出处)

Pick定理是说,假设平面上有一个顶点全在格点上的多边形P,那么其面积S(P)应该等于i+b/2-1,其中i为多边形内部所含的格点数,b是多边形边界上的格点数。绝大多数证明都是用割补的办法重新拼拆多边形。这里,我们来看一个另类的证明。
假设整个平面是一个无穷大的铁板;在0时间,每个格点上都有一个单位的热量。经过无穷长时间的传导后,最终这些热量将以单位密度均匀地分布在整个铁板上。下面我们试着求多边形P内的热量。考虑多边形的每一条线段e:它的两个端点均在格点上,因此线段e的中点是整个平面格点的对称中心,因而流经该线段的热量收支平衡(这半边进来了多少那半边就出去了多少),即出入该线段的热量总和实际为0。我们立即看到,P的热量其实完全来自于它自身内部的i个格点(的全部热量),以及边界上的b个格点(各自在某一角度范围内传出的热量)。边界上的b个点形成了一个内角和为(b-2)*180的b边形,从这b个点流入P的热量为(b-2)*180/360 = (b-2)/2 = b/2-1。再加上i个内部格点,于是S(P)=i+b/2-1。

代码:

/* ***********************************************
Author :rabbit
Created Time :2014/4/20 9:39:31
File Name :8.cpp
************************************************ */
#pragma comment(linker, "/STACK:102400000,102400000")
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include <string>
#include <time.h>
#include <math.h>
#include <queue>
#include <stack>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define eps 1e-10
#define pi acos(-1.0)
typedef long long ll;
int dcmp(double x){
	if(fabs(x)<eps)return 0;
	return x>0?1:-1;
}
struct Point{
	double x,y;
	Point(double _x=0,double _y=0){
		x=_x;y=_y;
	}
};
Point operator + (Point a,Point b){
	return Point(a.x+b.x,a.y+b.y);
}
Point operator - (Point a,Point b){
	return Point(a.x-b.x,a.y-b.y);
}
Point operator * (Point a,double p){
	return Point(a.x*p,a.y*p);
}
Point operator / (Point a,double p){
	return Point(a.x/p,a.y/p);
}
bool operator < (const Point &a,const Point &b){
	return a.x<b.x||(a.x==b.x&&a.y<b.y);
}
bool operator == (const Point &a,const Point &b){
	return dcmp(a.x-b.x)==0&&dcmp(a.y-b.y)==0;
}
double Dot(Point a,Point b){
	return a.x*b.x+a.y*b.y;
}
double Length(Point a){
	return sqrt(Dot(a,a));
}
double Angle(Point a,Point b){
	return acos(Dot(a,b)/Length(a)/Length(b));
}
double angle(Point a){
	return atan2(a.y,a.x);
}
double Cross(Point a,Point b){
	return a.x*b.y-a.y*b.x;
}
Point vecunit(Point x){
	return x/Length(x);
}
Point Normal(Point x){
	return Point(-x.y,x.x);
}
Point Rotate(Point a,double rad){
	return Point(a.x*cos(rad)-a.y*sin(rad),a.x*sin(rad)+a.y*cos(rad));
}
struct Line{
	Point p,v;
	double ang;
	Line(){}
	Line(Point P,Point v):p(P),v(v){
		ang=atan2(v.y,v.x);
	}
	bool operator < (const Line &L) const {
		return ang<L.ang;
	}
	Point point(double a){
		return p+(v*a);
	}
};
bool SegmentIntersection(Point a1,Point a2,Point b1,Point b2){
	double c1=Cross(a2-a1,b1-a1),c2=Cross(a2-a1,b2-a1),
		   c3=Cross(b2-b1,a1-b1),c4=Cross(b2-b1,a2-b1);
	return dcmp(c1)*dcmp(c2)<0&&dcmp(c3)*dcmp(c4)<0;
}
struct Node{
	ll x,y;
}pp[100100];
ll gcd(ll a,ll b){
	if(a==0)return b;
	return gcd(b%a,a);
}
ll xmult(Node a,Node b){
	return a.x*b.y-a.y*b.x;
}
int main()
{
	ll n;
	int T;
	cin>>T;
	for(int t=1;t<=T;t++){
		cin>>n;
		pp[0].x=pp[0].y=0;
		for(int i=1;i<=n;i++){
			ll x,y;
			scanf("%lld%lld",&x,&y);
			pp[i].x=pp[i-1].x+x;
			pp[i].y=pp[i-1].y+y;
		}
		ll s=0,e=0,q;
		for(int i=0;i<n;i++){
			s+=xmult(pp[i],pp[i+1]);
			ll dx=pp[i].x-pp[i+1].x;if(dx<=0)dx=-dx;
			ll dy=pp[i].y-pp[i+1].y;if(dy<=0)dy=-dy;
			e+=gcd(dx,dy);                                       
		}
		if(s<=0)s=-s;
		q=s/2+1-e/2;
		printf("Scenario #%d:\n",t);
		printf("%lld %lld %.1f\n",q,e,s/2.0);
		puts("");
	}
}



POJ 1265 pick定理,布布扣,bubuko.com

POJ 1265 pick定理

原文:http://blog.csdn.net/xianxingwuguan1/article/details/24272499

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!