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;
}

 

转载于:https://www.cnblogs.com/GBRgbr/p/3254707.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值