POJ 1654 Area (多边形面积)

题意:起点是左边原点,然后可以向八个方向移动,每次移动一格(或者对角线),保证最终回到原点。求移动一圈所围成的多边形的面积。

题解:实则是要我们求一个多边形的面积,原点在该多边形上。


#include<cstdio>
#include<cmath>
#include<cstring>

#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct Point { double x, y; };
int dir[12][2] = {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};

char str[1000007];
Point pre, now;

int main()
{
    int t;
    scanf("%d",&t);
    while ( t-- )
    {
        double s = 0, tmp;
        scanf("%s",str);
        for ( int i = 0; str[i] != '5'; i++ )
        {
            if ( i == 0 ) { pre.x = dir[str[i]-'0'][0]; pre.y = dir[str[i]-'0'][1]; continue; }
            now.x = pre.x + dir[str[i]-'0'][0];
            now.y = pre.y + dir[str[i]-'0'][1];
            tmp = now.x * pre.y - now.y * pre.x;
            pre.x = now.x; pre.y = now.y;
            s += tmp;
        }
        s = fabs(s) / 2;
        if ( zero(s-floor(s))) printf("%.0lf\n",s);
        else printf("%.1lf\n",s);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值