Area POJ - 1265(pick定律)

Area POJ - 1265(pick定律)

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

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表示多边形的面积。

而线上点的个数可用gcd,对两点(x1,y1),(x2,y2)。该线上点的个数E=gcd((x2-x1),(y2-y1))。

题意:从(0,0)出发每一步得到相应的位移距离从而得到各点,在求各点过程中求线上点的个数,求完各点后带入求多边形面积板子,再用pick定律求得即为求多边形内点的个数。

AC code

#include<cstdio>
#include<cmath>
#include<iostream>
#include<iomanip>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e3 + 5;
using namespace std;

const double pi = acos(-1.0);
const double inf = 1e100;
const double eps = 1e-6;
int sgn(double d) {
   
    if (fabs(d) < eps)
        return 0;
    if (d > 0)
        return 1;
    return -1;
}
int dcmp(double x, double y) {
   
    if (fabs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -1;
}

struct Point {
   
    double x, y;
    Point(double x = 0, double y = 0) :x(x), y(y) {
   }
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) {
   
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B) {
   
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) {
   
    return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p) {
   
    return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b) {
   
    if (a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}


bool operator == (const Point& a, const Point& b) {
   
    if (sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0)
        return true;
    return false;
}
double Dot(Vector A, Vector B) {
   
    return A.x * B.x + A.y * B.y;
}
double Length(Vector A) {
   
    return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B) {
   
    return acos(Dot(A, B) / Length(A) / Length(B));
}
double Cross(Vector A, Vector B) {
   
    return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C) {
   
    return Cross(B - A, C - B);
}
Vector Rotate(Vector A, double rad) {
   //rad为弧度 且为逆时针旋转的角
    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A) {
   //向量A左转90°的单位法向量
    double L = Length(A);
    return Vector(-A.y / L, A.x / L);
}
bool ToLeftTest(Point a, Point b, Point c) {
   
    return Cross(b - a, c - b) > 0;
}

double PolygonArea(Point* p, int n) {
   //p为端点集合,n为端点个数
    double s = 0;
    for (int i = 1; i < n - 1; ++i)
        s += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return s / 2;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b); }

Point lst[Max];
int main()
{
   
    FAST;
    int t;cin >> t;int g = 0;
    while (t--)
    {
   
        g++;
        int n;cin >> n;
        int x = 0, y = 0, I = 0, E = 0;
        lst[0].x = 0, lst[0].y = 0;
        for (int i = 1;i <= n;i++)
        {
   
            int a, b;cin >> a >> b;
            x += a, y += b;
            lst[i].x = x, lst[i].y = y;
            E += gcd(abs(a), abs(b));
        }
        double s = PolygonArea(lst, n+1);
        I = s + 1 - E / 2;
        cout << "Scenario #" << g << ":" << endl << I << " " << E << " " << fixed << setprecision(1) << s << " " << endl << endl;
    }
}
全部评论

相关推荐

到我怀里来:教育背景不用写主修课程,还有你写班级排名是什么意思?咋不写寝室排名呢😂要写就写年纪排名。得了那么多奖结果项目经历什么技术细节都不写清楚,把技术细节写清楚,用了什么技术解决了什么问题,“用了python语言、用了SQL语言”,有这样写的?hr一看就知道你是包装的或者比赛的奖是混的,你什么技术细节都不懂。校内职务全删了,什么三好学生、文明寝室这些你写上去干嘛?重复的奖学金你写三次干嘛?自我评价写那么多干嘛?谁想看这些
点赞 评论 收藏
分享
2024-11-11 09:31
香港中文大学 前台
Hello_WordN:咱就是说,除了生命其他都是小事,希望面试官平安,希望各位平时也多注意安全
点赞 评论 收藏
分享
2024-12-29 19:48
河北科技大学 Java
蜂在图书:河北科技大学
点赞 评论 收藏
分享
评论
1
收藏
分享
牛客网
牛客企业服务