poj 1265 Area

来源:互联网 发布:表格相同时间数据提取 编辑:程序博客网 时间:2024/06/14 10:26
Area
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6789 Accepted: 2930

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. 

 
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

241 00 1-1 00 -175 01 3-2 2-1 00 -3-3 10 -3

Sample Output

Scenario #1:0 4 1.0Scenario #2:12 16 19.0

Source

Northwestern Europe 2001

这题开始不会,看大牛博客后就明白了,原文链接为:http://blog.csdn.net/lin375691011/article/details/17765151
题意:给一个n边形,计算n边形内的整点的个数m,n边形边经过的整点个数p(含边的顶点),以及n边形的面积a
这题的几个要点:
1.以格子点为顶点的线段,覆盖的点的个数为GCD(dx,dy),其中,dxdy分别为线段横向占的点数和纵向占的点数。如果dx或dy为0,则覆盖的点数为dy或dx。通过这个可以求得边上点的个数。
2.计算多边形的面积a直接由相邻两个点的叉乘和的绝对值得到。
3.pick公式:平面上以格子点为顶点的简单多边形的面积=边上的点数/2+内部的点数-1。
知道这些代码就不难了。

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string>#include<stack>#include<queue>#include<deque>#include<set>#include<map>#include<cmath>#include<vector>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int, int> PII;#define pi acos(-1.0)#define eps 1e-10#define pf printf#define sf scanf#define lson rt<<1,l,m#define rson rt<<1|1,m+1,r#define e tree[rt]#define _s second#define _f first#define all(x) (x).begin,(x).end#define mem(i,a) memset(i,a,sizeof i)#define for0(i,a) for(int (i)=0;(i)<(a);(i)++)#define for1(i,a) for(int (i)=1;(i)<=(a);(i)++)#define mi ((l+r)>>1)#define sqr(x) ((x)*(x))const int inf=0x3f3f3f3f;int T,n;struct Point{    int x,y;}l[1001];int cross(Point a,Point b)//计算叉乘{    return a.x*b.y-a.y*b.x;}int gcd(int x,int y){    return !y?x:gcd(y,x%y);}int main(){    sf("%d",&T);    for1(i,T)    {        int p=0;        int a=0;        l[0].x=0,l[0].y=0;        sf("%d",&n);        for1(i,n)        {            sf("%d%d",&l[i].x,&l[i].y);            int dy=abs(l[i].x),dx=abs(l[i].y);            p+=gcd(dy,dx);            l[i].x+=l[i-1].x,l[i].y+=l[i-1].y;//每次输入的点实际是相对于上一次走的距离,并不是实际坐标,所以应该累加,计算gcd的时用到的是相对距离所以跟实际坐标无关            a+=cross(l[i],l[i-1]);        }        a=abs(a);        pf("Scenario #%d:\n",i);        pf("%d %d %.1f\n\n",a-p+2>>1,p,0.5*a);    }    return 0;}


原创粉丝点击