Water Testing【皮克定理,多邊形面積,線段上點的數目】

Water Testing

傳送門:鏈接  來源:UPC 9656

題目描述

You just bought a large piece of agricultural land, but you noticed that – according to regulations – you have to test the ground water at specific points on your property once a year. Luckily the description of these points is rather simple. The whole country has been mapped using a Cartesian Coordinate System (where (0, 0) is the location of the Greenwich Observatory). The corners of all land properties are located at integer coordinates according to this coordinate system. Test points for ground water have to be erected on every point inside a property whose coordinates are integers.

輸入

The input consists of:
• one line with a single integer n (3 ≤ n ≤ 100 000), the number of corner points of your property;
• n lines each containing two integers x and y (−106 ≤ x, y ≤ 106 ), the coordinates of each corner.
The corners are ordered as they appear on the border of your property and the polygon described by the points does not intersect itself.

輸出

The number of points with integer coordinates that are strictly inside your property.

樣例輸入

4
0 0
0 10
10 10
10 0

樣例輸出

81

題目大意:

給出一個多邊形每個頂點的座標,求在該圖形內部方格點的個數。

解題思路:

`1、皮克定理:2S=b+2a-2   (其中S表示多邊形的面積,b表示多邊形上點的個數,a表示多邊形內部點的個數。)

2、已知頂點座標求多邊形面積公式S=0.5*abs(x1*y2-y1*x2+x2*y3-y2*x3+...+xn*y1-yn*x1)

3、已知方向向量爲(x,y)求在線段上點的個數b=gcd(fabs(x),fabs(y))

如果(x1,y1)只是一個頂點而不是向量,就要先求出邊的向量才能用公式3!!!

根據上面三個公式可以求出: b=S-1+0.5*a

qi shi shu lun bu gui wo guan ! 

AC代碼:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL MAX=1e5;
struct point{ 
    double x;
    double y;
}p[MAX+5];
LL gcd(LL a,LL b)
{
    if(b==0) return a;
    return gcd(b,a%b);
}
int main()
{
    LL n;
    cin>>n;
    double s=0,b=0;
    for(LL i=0;i<n;i++){
        cin>>p[i].x>>p[i].y;
        if(i!=0){
            b+=gcd(fabs(p[i].x-p[i-1].x),fabs(p[i].y-p[i-1].y));
            s+=(p[i].y*p[i-1].x-p[i].x*p[i-1].y);
        }
    }
    s+=(p[0].y*p[n-1].x-p[0].x*p[n-1].y);
    b+=gcd(fabs(p[0].x-p[n-1].x),fabs(p[0].y-p[n-1].y)); 
    s=0.5*fabs(s);
    cout<<(LL)(s+1-b*0.5)<<endl;
    return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章