多边形面积(1654)

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct Point
{
    int x,y;
    Point(){}
    Point(int x,int y):x(x),y(y){}
};
typedef Point Vector;
Vector operator-(Point A,Point B)
{
    return Vector(A.x-B.x, A.y-B.y);
}
long long Cross(Vector A,Vector B)
{
    return A.x*B.y-A.y*B.x;
}
 
long long PolygonArea(Point *p,int n)//返回多边形面积的双倍
{
    long long area=0;
    for(int i=1;i<n-1;++i)
        area += Cross(p[i]-p[0],p[i+1]-p[0]);
    return area<0?-area:area;
}
 
const int dx[]={0,-1,0,1,-1,0,1,-1,0,1};//
const int dy[]={0,-1,-1,-1,0,0,0,1,1,1};
const int maxn=1000000+5;
char str[maxn];
Point p[maxn];
int n;
int main()
{
    int T; scanf("%d%*c",&T);
    while(T--)
    {
        n=0;
        p[0]=Point(0,0);
        scanf("%s",str);
        n=strlen(str)-1;
        for(int i=0;i<n-1;++i)
            p[i+1]=Point(p[i].x+dx[str[i]-'0'], p[i].y+dy[str[i]-'0']);
        long long ans=PolygonArea(p,n);
        if(ans%2==0)printf("%I64d\n",ans/2);
        else printf("%I64d.5\n",ans/2);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值