poj 1265 Pick定理(模版题)

题意:在网格上给出一个机器人监控的行走路线,开始是从(0,0)出发,每一次在前一步的基础上
向右走dx步,向上走dy步,最终走回起点,求路线形成的多边形的边上、内部的点的个数以及该
多边形的面积

题解:pick定理(模板题)

 

#include <iostream>

using namespace std;

struct point
{
    int x,y;
};

point points[105];

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

//多边形上的网格点个数
int grid_onedge(int n,point* p)
{
    int i,ret=0;
    for (i=0;i<n;i++)
        ret+=gcd(abs(p[i].x-p[(i+1)%n].x),abs(p[i].y-p[(i+1)%n].y));
    return ret;
}

//多边形内的网格点个数
int grid_inside(int n,point* p)
{
    int i,ret=0;
    for (i=0;i<n;i++)
        ret+=p[(i+1)%n].y*(p[i].x-p[(i+2)%n].x);
    return (abs(ret)-grid_onedge(n,p))/2+1;
}

int main(void)
{
    int t,cas;
    cin >> t;
    for(cas=1; cas<=t; cas++)
    {
        int m,dx,dy;
        cin >> m;
        cin >> points[0].x >> points[0].y;
        for(int i=1; i<m; i++)
        {
            cin >> dx >> dy;
            points[i].x = points[i-1].x + dx;
            points[i].y = points[i-1].y + dy;
        }

        int ipt,ept;
        double area;
        ipt = grid_inside(m,points);
        ept = grid_onedge(m,points);
        area = ipt + ept/2.0 - 1;
        cout << "Scenario #" << cas <<  ":" << endl;
        printf("%d %d %.1lf\n\n",ipt,ept,area);
    }
    return 0;
}

 

posted @ 2014-03-20 23:23  辛力啤  阅读(185)  评论(0编辑  收藏  举报