您的位置:首页 > 其它

ZOJ1032-Area 2(已知点坐标求多边形面积,求线段上的整点数量,pick定理)

2017-04-19 20:06 357 查看
Area 2
Time Limit: 2 Seconds      Memory Limit: 65536 KB

Background

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��s 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.



Figure 1: Example area.
Problem

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 ��dx 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 ��Scenario #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

Source: Northwestern Europe 2001

题意:告诉你n边形的n个点坐标,求n边形的面积,n边形边界上的整点数,内部的整点数

解题思路:Pick定理 设以整数点为顶点的多边形的面积为S, 多边形内部的整数点数为N, 多边形边界上的整数点数为L, 则 N + L/2 - 1 = S

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <set>
#include <stack>
#include <map>
#include <functional>
#include <string>

using namespace std;

#define LL long long
#define INF 0x3f3f3f3f
#define N 110
const double pi=acos(-1.0);

struct point
{
double x,y;
}p
;

int gcd(int a,int b)
{
int tmp;
while((a%b)!=0)
{
tmp=a%b;
a=b;
b=tmp;
}
return b;
}

int intp_insegment(point a,point b) // point
{
int aa,bb;
aa=abs((int)(b.y-a.y));
bb=abs((int)(b.x-a.x));
if(!aa && !bb)return 0;
if(!aa)return bb-1;
if(!bb)return aa-1;
return gcd(aa,bb)-1;
}

double area_polygon(point p[],int n) // area
{
double s1=0,s2=0;
for(int i=0;i<n;i++)
{
s1+=p[(i+1)%n].y*p[i].x;
s2+=p[(i+1)%n].y*p[(i+2)%n].x;
}
return fabs(s1-s2)/2.0;
}

int main()
{
int t;
scanf("%d",&t);
int cas=0;
while(t--)
{
printf("Scenario #%d:\n",++cas);
p[0].x=p[0].y=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
p[i].x=p[i-1].x+a;
p[i].y=p[i-1].y+b;
}
int cnt=intp_insegment(p[0],p[n-1]);
for(int i=0;i<n-1;i++)cnt+=intp_insegment(p[i],p[i+1]);
cnt+=n;
double s=area_polygon(p,n);
int x=(int)s+1-cnt/2;
printf("%d %d %.1lf\n\n",x,cnt,s);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: