1. 程式人生 > >1265 Area(pick公式)

1265 Area(pick公式)

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. 

                                          

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

題目大意:從起始點開始,給出機器人每次在方格上移動的橫座標變化量和縱座標變化量,求移動軌跡所形成的多邊形的內部格點,邊上格點,面積。

解題思路:使用pick公式,pick公式為:

 使用叉乘求多邊形的面積,gcd求邊上的格點數,pick求內部的格點數

AC程式碼:

使用G++提交會WA,用c++提交

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio> 
using namespace std;
const int maxn=1e3;
struct point{
	int x,y;
}a[maxn];
int det(point a,point b)//叉乘 
{
	return a.x*b.y-a.y*b.x;
}
int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
} 
double area(point *ch,int m)//計算多邊形的面積 ,凸多邊形和非凸多邊形均適用 
{
	double sum=0;
	for(int i=1;i<m-1;i++)
	{
		sum+=det(ch[i],ch[i+1]);
	}
	return fabs(sum/2);
}
int main()
{
	int n,dx,dy,t;
	int ans1;//邊上的格點數
	int ans2;//內部的格點數 
	double sum;//面積 
	a[0].x=0,a[0].y=0;//從將座標原點作為起始點 
	cin>>t;
	int k=1;
	while(t--)
	{
		ans1=0;
		sum=0;
		cin>>n;
		for(int i=1;i<=n;i++)
		{
			cin>>dx>>dy;
			a[i].x=a[i-1].x+dx;
			a[i].y=a[i-1].y+dy;
			ans1+=gcd(abs(a[i].x-a[i-1].x),abs(a[i].y-a[i-1].y));//邊上格點數 
		}
		printf("Scenario #%d:\n",k++);
		sum=area(a,n);//因為時閉合曲線,從座標原點開始,最後一定會回到座標原點,所以一共有n個頂點; 
		ans2=sum-1.0*ans1/2+1;
		printf("%d %d %.1lf\n\n",ans2,ans1,sum); 
	}
	return 0; 
}