LightOJ 1418 Trees on My Island (Pick定理)

题目链接:LightOJ 1418

Problem Description

I have bought an island where I want to plant trees in rows and columns. So, the trees will form a rectangular grid and each of them can be thought of having integer coordinates by taking a suitable grid point as the origin.

But, the problem is that the island itself is not rectangular. So, I have identified a simple polygonal area inside the island with vertices on the grid points and have decided to plant trees on grid points lying strictly inside the polygon.

Figure: A sample of my island

For example, in the above figure, the green circles form the polygon, and the blue circles show the position of the trees.

Now, I seek your help for calculating the number of trees that can be planted on my island.

Input

Input starts with an integer \(T (≤ 100)\), denoting the number of test cases.

Each case starts with a line containing an integer \(N (3 ≤ N ≤ 10000)\) denoting the number of vertices of the polygon.

Each of the next \(N\) lines contains two integers \(x_i y_i (-10^6 ≤ x_i, y_i ≤ 10^6)\) denoting the co-ordinate of a vertex. The vertices will be given in clockwise or anti-clockwise order. And they will form a simple polygon.

Output

For each case, print the case number and the total number of trees that can be planted inside the polygon.

Sample Input

1

9

1 2

2 1

4 1

4 3

6 2

6 4

4 5

1 5

2 3

Sample Output

Case 1: 8

Note

Dataset is huge, use faster I/O methods.

Solution

题意:

给定一个多边形,顶点都在格点上,求多边形内部的格点个数。

思路

Pick 定理 裸题。

#include <cstdio>
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef double db;
const db eps = 1e-10;
const db pi = acos(-1.0);
const ll inf = 0x3f3f3f3f3f3f3f3f;
const ll maxn = 1e5 + 10;

inline int dcmp(db x) {
    if(fabs(x) < eps) return 0;
    return x > 0? 1: -1;
}

class Point {
public:
    ll x, y;
    Point(ll x = 0, ll y = 0) : x(x), y(y) {}
    void input() {
        scanf("%lld%lld", &x, &y);
    }
    Point operator-(const Point a) {
        return Point(x - a.x, y - a.y);
    }
    ll cross(const Point a) {
        return x * a.y - y * a.x;
    }
};

Point p[maxn];

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

int main() {
    int T;
    scanf("%d", &T);
    for(int _ = 1; _ <= T; ++_) {
        int n;
        scanf("%d", &n);
        ll on = 0;
        ll s = 0;
        for(int i = 0; i < n; ++i) {
            p[i].input();
        }
        p[n] = p[0];
        for(int i = 0; i < n; ++i) {
            s += (p[i + 1] - p[0]).cross(p[i] - p[0]);
            on += gcd(abs(p[i].x - p[i + 1].x), abs(p[i].y - p[i + 1].y));
        }
        s = abs(s);
        ll in = s / 2 - on / 2 + 1;
        printf("Case %d: ", _);
        printf("%lld\n", in);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/wulitaotao/p/11846152.html

时间: 2024-10-12 15:40:39

LightOJ 1418 Trees on My Island (Pick定理)的相关文章

UVa 10088 - Trees on My Island (pick定理)

样例: 输入:123 16 39 28 49 69 98 96 55 84 43 51 3121000 10002000 10004000 20006000 10008000 30008000 80007000 80005000 40004000 50003000 40003000 50001000 300040 01000000 01000000 10000000 100000040 0100 0100 1000 100 输出: 21 25990001 999998000001 9801 分析

poj 1265 Area(Pick定理)

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5666   Accepted: 2533 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

poj 1265 Area (Pick定理+求面积)

链接:http://poj.org/problem?id=1265 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4969   Accepted: 2231 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionag

poj 1265 Area(pick 定理)

链接:poj 1265 题意:从原点出发.给出一些dx,dy移动增量,终于形成一个多边形, 求多边形内部的格点数目,边上的格点数目 .以及面积. 补充知识: 1.以格子点为顶点的线段.覆盖的点的个数为gcd(|dx|,|dy|).当中,|dx|,|dy|分别为线段横向增量和纵向增量. 2.Pick定理:设平面上以格子点为顶点的多边形的内部点个数为a.边上点个数为b.面积为S,则 S = a + b/2 -1. 3.随意一个多边形的面积等于以多边形边上的某点为固定点.按顺序求其余点相邻两个点与该点

POJ 1265 Area POJ 2954 Triangle Pick定理

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5227   Accepted: 2342 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

pick定理:面积=内部整数点数+边上整数点数/2-1

1 //pick定理:面积=内部整数点数+边上整数点数/2-1 2 // POJ 2954 3 4 #include <iostream> 5 #include <cstdio> 6 #include <cstdlib> 7 #include <algorithm> 8 #include <vector> 9 #include <math.h> 10 using namespace std; 11 #define LL long lo

POJ1265——Area(Pick定理+多边形面积)

Area DescriptionBeing 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 surveilla

PICK定理

PICK定理: S=I+O/2-1 S为多边形面积,I多边形内部的格点,O是多边形边上的格点 其中边上格点求法: 假设两个点A(x1,y1),B(x2,y2) 线段AB间格点个数为gcd(abs(x1-x2),abs(y1-y2))-1 特判x1-x2==0 或者 y1-y2==0,则覆盖的点数为 y2-y1 或 x2-x1 POJ 2594 Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5106  

poj 2954 Triangle(Pick定理)

链接:http://poj.org/problem?id=2954 Triangle Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5043   Accepted: 2164 Description A lattice point is an ordered pair (x, y) where x and y are both integers. Given the coordinates of the vertices