Area---poj1265(皮克定理+多边形求面积)

题目链接:http://poj.org/problem?id=1265

题意是:有一个机器人在矩形网格中行走,起始点是(0,0),每次移动(dx,dy)的偏移量,已知,机器人走的图形是一个多边形,求这个机器人在网格中所走的面积,还有就是分别求多边形上和多边形内部有多少个网格点;

皮克定理:

  在一个多边形中。用I表示多边形内部的点数,E来表示多边形边上的点数,S表示多边形的面积。

  满足:S:=I+E/2-1;

求E,一条边(x1,y1,x2,y2)上的点数(包括两个顶点)=gcd(abs(x1-x2),abs(y1-y2));

求S用差积

 类似的题还有poj2954

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<queue>

using namespace std;

#define met(a, b) memset(a, b, sizeof(a))
#define N 111

typedef long long LL;

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

int main()
{
    int T, t = 1, n;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d", &n);

        int prex = 0, prey = 0, x, y, S = 0, E = 0;

        for(int i=1; i<=n; i++)
        {
            scanf("%d %d", &x, &y);///x和y是偏移量;

            E += gcd(abs(x), abs(y));///求边上的格点数;

            x += prex;
            y += prey;///求现在的点坐标;

            S += x*prey-y*prex;///做差积;

            prex = x;
            prey = y;///更新前一个点;
        }
        printf("Scenario #%d:\n", t++);
        printf("%d %d %.1f\n\n", (abs(S)-E)/2 + 1, E, abs(S)/2.0);///利用S=I+E/2-1;
    }
    return 0;
}
View Code

 

posted @ 2016-08-01 11:05  西瓜不懂柠檬的酸  Views(260)  Comments(0Edit  收藏  举报
levels of contents