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;
    }
}
全部评论

相关推荐

bg双非本科,方向是嵌入式。这次秋招一共拿到了&nbsp;8&nbsp;个&nbsp;offer,最高年包&nbsp;40w,中间也有一段在海康的实习经历,还有几次国家级竞赛。写这篇不是想证明什么,只是想把自己走过的这条路,尽量讲清楚一点,给同样背景的人一个参考。一、我一开始也很迷茫刚决定走嵌入式的时候,其实并没有一个特别清晰的规划。网上的信息很零散,有人说一定要懂底层,有人说项目更重要,也有人建议直接转方向。很多时候都是在怀疑:1.自己这种背景到底有没有机会2.现在学的东西到底有没有用3.是不是已经开始晚了这些问题,我当时一个都没答案。二、现在回头看,我主要做对了这几件事第一,方向尽早确定,但不把自己锁死。我比较早就确定了嵌入式这个大方向,但具体做哪一块,是在项目、竞赛和实习中慢慢调整的,而不是一开始就给自己下结论。第二,用项目和竞赛去“证明能力”,而不是堆技术名词。我不会刻意追求学得多全面,而是确保自己参与的每个项目,都能讲清楚:我负责了什么、遇到了什么问题、最后是怎么解决的。第三,尽早接触真实的工程环境。在海康实习的那段时间,对我触动挺大的。我开始意识到,企业更看重的是代码结构、逻辑清晰度,以及你能不能把事情说清楚,而不只是会不会某个知识点。第四,把秋招当成一个需要长期迭代的过程。简历不是一次写完的,面试表现也不是一次就到位的。我会在每次面试后复盘哪些问题没答好,再针对性补。三、我踩过的一些坑现在看也挺典型的:1.一开始在底层细节上纠结太久,投入产出比不高2.做过项目,但前期不会总结,导致面试表达吃亏3.早期有点害怕面试,准备不充分就去投这些弯路走过之后,才慢慢找到节奏。四、给和我背景相似的人一点建议如果你也是双非,准备走嵌入式,我觉得有几件事挺重要的:1.不用等“准备得差不多了”再投2.项目一定要能讲清楚,而不是做完就算3.不要只盯着技术,多关注表达和逻辑很多时候,差的不是能力,而是呈现方式。五、写在最后这篇总结不是标准答案,只是我个人的一次复盘。后面我会陆续把自己在嵌入式学习、竞赛、实习和秋招中的一些真实经验拆开来讲,希望能对后来的人有点帮助。如果你正好也在这条路上,希望你能少走一点弯路。
x_y_z1:蹲个后续
点赞 评论 收藏
分享
2025-12-09 16:37
西北大学 前端工程师
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务