1. 程式人生 > >POJ 1265 (Pick 公式+求任意多邊形面積+頂點多邊形的邊整點個數)

POJ 1265 (Pick 公式+求任意多邊形面積+頂點多邊形的邊整點個數)

Area
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 7049 Accepted: 3025
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
Source

Northwestern Europe 2001

題意:給出N個位移向量,求形成的封閉多邊形面積和內部整點個數以及邊上的整點個數

做法:
1.對於任意一個整點多邊形, 有Pick 公式:S=a+b/2-1,S是面積,a是內點個數,b是邊上的整點個數
2、對於一條端點都在整點的線段,其跨過的整點個數是gcd(dx,dy)
dx=abs(st.x-ed.x),dy=abs(st.y-ed.y)
當dx=0時就是dy 當dy=0時就是dx
3.求任意多邊形面積:
(1)將頂點進行極角排序
(2)求出任意相鄰兩頂點叉積的和(共n對)再除以2
由於此題的輸入順序是逆時針方向的,所以本來就可以不排序了

這題比較坑的是用%.1f才能過。。。。

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <map>
#include <stack>
#include <vector>
#define maxn 10010
#define maxe 100010
typedef long long ll;
using namespace std;
const double eps=1e-5;
const int inf=0x3f3f3f3f3f;
typedef double T1;
struct Point
{
    T1 x,y;
    Point(){};
    Point(T1 a,T1 b)
    {
        x=a,y=b;
    }
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
    Point operator +(Point a)
    {
        Point b(x+a.x,y+a.y);
        return b;
    }
    Point operator -(Point a)
    {
        Point b(x-a.x,y-a.y);
        return b;
    }
    T1 operator *(Point a)
    {
        return x*a.x+y*a.y;
    }
    T1 operator ^(Point a)
    {
        return x*a.y-y*a.x;
    }
    bool operator <(Point a)
    {
        return x<a.x;
    }
}p[maxn];
double xmult(Point p0,Point p1,Point p2)
{
    return (p1-p0)^(p2-p0);
}
int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
int solve(int a,int b)
{
    a=abs(a);b=abs(b);
    if(a==0)return b;
    if(b==0)return a;
    return gcd(a,b);
}
int main()
{
    int t;
    int ca=1;
    //freopen("in.txt","r",stdin);
    scanf("%d",&t);
    int n;
    int a,b;
    while(t--)
    {
        scanf("%d",&n);
        int e=0;
        scanf("%d%d",&a,&b);
        e+=solve(a,b);
        p[0]=Point(a,b);
        Point delta;
        for(int i=1;i<n;i++)
        {
            scanf("%d%d",&a,&b);
            delta=Point(a,b);
            e+=solve(a,b);
            p[i]=p[i-1]+delta;
        }
        double area=0.0;
        for(int i=0;i<n;i++)
        {
            int j=(i+1)%n;
            area+=(p[i]^p[j]);
        }
        area=fabs(area)/2;
        int i=area+1-e/2.0;
        printf("Scenario #%d:\n",ca++);
        printf("%d %d %.1f\n\n",i,e,area);
    }
    return 0;
}

相關推薦

POJ 1265 (Pick 公式+任意多邊形面積+頂點多邊形整點個數)

Area Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7049 Accepted: 3025 Description Being well known for i

poj 1125 Floyd演算法任意兩點間的最短路

剛開始的時候一直不明白插點的順序為什麼不會對最後的結果有影響----一直不懂這個演算法--- 今天看見他們都學會了-.-終於把我也帶會了-- 對於原理的理解我還是通過輸出每一步更新後的結果搞明白的 開始一直不懂的是: 對於A-D: 更新B時--AB+BD>A

poj 2395Out of Hay(最小的最大權 瓶頸生成樹)

Out of Hay Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19901  

hdu-2036任意多邊形面積

改革春風吹滿地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 45028   

任意多邊形面積(凹多邊形和凸多邊形

遇到問題:已知多邊形的各個左邊點,要求多邊形的面積 然後我搜索了下看到這篇文章:https://blog.csdn.net/tianyuhang123/article/details/56094559 這個人說的不多,但是簡單明瞭: 首先已知各定點的座標分別為(x1,y1),(x2,y2

POJ 2826 An Easy Problem?! 叉積多邊形面積 【計算幾何】

An Easy Problem?! Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7837 Accepted: 1145 Description It's

計算幾何工具演算法-任意多邊形面積

題目描述: 給出n邊形的n個頂點座標,求這個n邊形的面積 題目分析: 如果在數學上,大概會把這個多邊形分成三角形(n-2)個三角形來求 但是這樣免不了繁瑣的演算法,程式設計複雜度和時間複雜度

任意多邊形面積-有向面積

給定多邊形的頂點座標(有序),讓你來求這個多邊形的面積,你會怎麼做? 我們知道,任意多邊形都可以分割為N個三角形,所以,如果以這為突破點,那麼我們第一步就是把給定的多邊形,分割為數個三角形,分別求面積,最後累加就可以了,把多邊形分割為三角形的方式多種多樣,在這裡,我們按照如

hdoj-2036題解-向量積法任意多邊形面積

該題題意就是逆時針給出點的座標,求這個多邊形的面積。下面就寫一下如何用向量積法求多邊形面積。 向量積法與面積 上圖說明了如何利用向量求得三角形的面積,下面介紹一下所謂的右手法則: 如圖,2個三角形ABC,唯一的區別在於上面的三角形ABC的標識是逆時針,而下面的

任意多邊形面積與重心的演算法

多邊形重心問題時間限制:3000 ms  |  記憶體限制:65535 KB難度:5描述在某個多邊形上,取n個點,這n個點順序給出,按照給出順序將相鄰的點用直線連線, (第一個和最後一個連線),所有線段不和其他線段相交,但是可以重合,可得到一個多邊形或一條線段或一個多邊形

任意多邊形面積(模板題)

題目描述 眾所周知的是,小X特別喜歡由線條組成的形狀,比如說凸多邊形,這一天小X正在紙上畫著喜歡的凸多邊形,這時候小Y走了過來,指著一張圖片上的多邊形,問小X知道這個圖形的面積麼,這可把小X難住了,聰明的你一定能夠幫助小X解決這個問題,對吧~ 輸入描述: 多邊形上最多有

向量叉乘任意多邊形面積

多邊形面積在計算機中有一個很好的處理辦法就是相量叉乘,我們知道三角形ABC的面積可以等於1/2*|AB|*|AC|*sinABC,也就是等於ABXAC的模 若A(x1,y1),B(x2,y2),那麼2S=x1*y2-x2*y1。把n邊形分成n-2個三角形,題目給的頂點座標都

poj 1265 Area(pick 定理)

clu mod href 格子 .org ble gb2312 gb2 col 鏈接:poj 1265 題意:從原點出發,給出一些dx,dy移動增量,終於形成一個多邊形, 求多邊形內部的格點數目,邊上的格點數目 ,以及面積。 補充知識: 1、以格子點為頂點的線段。覆蓋

求解多邊形面積2S= Σ【Xi (Yi+1-Yi-1)】,(i屬於1~n),公式解析及編程實現

poi logs 驗證 地圖 class view hide 對比 turn yogurt今天要個大家分享一個基礎的二維空間多邊形面積求算方法,主要也是為了下一篇《橢球體上某區域面積的求算,及Albers投影與墨卡托投影後該區域面積對比》打一個基礎。關於投影的相關過

POJ 3907 Build Your Home | 計算多邊形面積

line cstring i+1 return 多邊形 ble def != poi 給個多邊形 計算面積 輸出要四舍五入 直接用向量叉乘就好 四舍五入可以+0.5向下取整 #include<cstdio> #include<algorithm>

[poj] 3348 Cows || 凸包面積

post sort while swa 多邊形 sca cpp name graham 原題 給出n個點,求得到凸包的面積 多邊形面積顯然很好求,就是鄰邊叉積之和/2。 問題在於怎麽求凸包上有哪些點。凸包顯然每個點都要在前兩個點連線的左邊(也就是逆時針位置),所以: 1、

nyoj-3-多邊形重心問題(多邊形面積和中心)

truct abs 算法 online ble ans sin 競賽 poi 題目鏈接 1 /* 2 Name:nyoj-3-多邊形重心問題 3 Copyright: 4 Author: 5 Date: 2018/4/

HDU 2036 改革春風吹滿地 (多邊形面積

tput %d 回老家 show .cn 三分 strong otto 而且 傳送門: 改革春風吹滿地 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To

Cows 計算幾何 凸包 多邊形面積

operator head opera -a ack ros mean lock rom 題目鏈接:https://cn.vjudge.net/problem/POJ-3348 題意 啊模版題啊 求凸包的面積,除50即可 思路 求凸包的面積,除50即可 提交過程 AC

hdu 2036 多邊形面積 (凸、凹多邊形

node 開始 責任 clas 簡化 memset abs 得到 algo <題目鏈接> Problem Description “ 改革春風吹滿地,不會AC沒關系;實在不行回老家,還有一畝三分地。謝謝!(樂隊奏樂)”話說部分學生心態極好,每天就知道遊戲,這