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

1418 - Trees on My Island
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 next N 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;
}






1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行;、 2项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值