1418 - Trees on My Island ( Peake's theorem )

来源:互联网 发布:五金进入货软件 编辑:程序博客网 时间:2024/06/04 19:48
1418 - Trees on My Island
PDF (English)StatisticsForum
Time Limit: 5 second(s)Memory Limit: 32 MB

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

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


Figure: A sample ofmy island

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

Now, I seek your help for calculating the number of treesthat 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 nextN lines contains two integers xi yi(-106 ≤ xi, yi ≤ 106)denoting the co-ordinate of a vertex. The vertices will be given in clockwiseor anti-clockwise order. And they will form a simple polygon.

Output

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


题解:

求点阵多边形内整点个数, 由皮克定理:

一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,S表示多边形的面积。  -- 百度百科

多边形面积 S  由叉积容易求出,两条边上的点数,也是欧几里得算法的应用。故有皮克定理,a很容易求出。

/*********************************************** * Author: fisty * Created Time: 2015-10-11 16:15:25 * File Name   : lightoj1418.cpp *********************************************** */#include <iostream>#include <cstring>#include <deque>#include <cmath>#include <queue>#include <stack>#include <list>#include <map>#include <set>#include <string>#include <vector>#include <cstdio>#include <bitset>#include <algorithm>using namespace std;#define Debug(x) cout << #x << " " << x <<endl#define Memset(x, a) memset(x, a, sizeof(x))const int INF = 0x3f3f3f3f;typedef long long LL;typedef pair<int, int> P;#define FOR(i, a, b) for(int i = a;i < b; i++)#define lson l, m, k<<1#define rson m+1, r, k<<1|1#define MAX_N 11000    int t, n;struct node{       LL x, y;   }p[MAX_N];    LL gcd(LL a, LL b){    return b == (LL)0 ? a : gcd(b, a % b);}LL cross(struct node e1, struct node e2, struct node e3){    return (e2.x - e1.x) * (e3.y - e1.y) - (e3.x - e1.x ) * (e2.y - e1.y);}int main() {    //freopen("in.cpp", "r", stdin);    //cin.tie(0);    //ios::sync_with_stdio(false);    scanf("%d", &t);    int cnt = 1;    while(t--){        scanf("%d", &n);                for(int i = 0;i < n; i++){            scanf("%lld%lld", &p[i].x, &p[i].y);        }        p[n] = p[0];        LL count = 0;        for(int i = 0;i < n; i++){            LL a = abs(p[i+1].x - p[i].x);            LL b = abs(p[i+1].y - p[i].y);            LL c = gcd(a, b);            count += c;            }                LL area = 0;        for(int i = 1;i < n; i++){            area += cross(p[0], p[i], p[i+1]);        }        printf("Case %d: %lld\n",cnt++, abs(area  / 2) + 1 - count / 2);    }    return 0;}






0 0
原创粉丝点击