HDU 3775 Chain Code pick定理

pick定理:一个计算点阵中顶点在格点上的多边形面积公式:S=a+b÷2-1,其中a表示多边形内部的点数,b表示多边形边界上的点数,s表示多边形的面积。

思路:http://blog.csdn.net/magicnumber/article/details/6192242

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL long long int

using namespace std;

const int MAXN = 1000100;
const LL dx[] = { 0, -1, -1, -1,  0,  1, 1, 1 };
const LL dy[] = { 1,  1,  0, -1, -1, -1, 0, 1 };

struct Point
{
    LL x, y;
    Point( LL x = 0, LL y = 0 ):x(x), y(y) { }
};
typedef Point Vector;

Vector operator+( Vector A, Vector B )       //向量加
{
    return Vector( A.x + B.x, A.y + B.y );
}

Vector operator-( Vector A, Vector B )       //向量减
{
    return Vector( A.x - B.x, A.y - B.y );
}

Vector operator*( Vector A, double p )      //向量数乘
{
    return Vector( A.x * p, A.y * p );
}

Vector operator/( Vector A, double p )      //向量数除
{
    return Vector( A.x / p, A.y / p );
}

char str[MAXN];
Point P[MAXN];

LL Cross( Vector A, Vector B )   //向量叉积
{
    return A.x * B.y - A.y * B.x;
}

LL PolygonArea( Point *p, int n )   //多边形有向面积
{
    LL area = 0;
    for ( int i = 1; i < n - 1; ++i )
        area += Cross( p[i] - p[0], p[i + 1] - p[0] );
    return area;
}

int main()
{
    while ( scanf( "%s", str ) == 1 )
    {
        int n = strlen(str);
        Point st = Point(0, 0);
        for ( int i = 0; i < n; ++i )
        {
            st.x += dx[ str[i] - '0' ];
            st.y += dy[ str[i] - '0' ];
            P[i] = st;
        }

        LL s = PolygonArea( P, n );
        if ( s < 0 ) s = -s;

        printf("%I64d\n", (s + n) / 2 + 1 );
    }
    return 0;
}

 

posted @ 2013-08-13 10:57  冰鸮  阅读(228)  评论(0编辑  收藏  举报