POJ 1654 Area

Description
You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2.
For example, this is a legal polygon to be computed and its area is 2.5:


【题目分析】
其实就是一道拆分下来的叉积求面积的小水题目。只需要扫一遍就可以了,注意叉积相减的方式。刚开始因为写反了,结果WA了好多次。而且还要加入特判,当字符串长度小于3时要输出0。(因为要回到原点)


【代码】

#include <cstdio>
#include <cstring>
int mov[10][2]={{0,0},{1,-1},{1,0},{1,1},{0,-1},{0,0},{0,1},{-1,-1},{-1,0},{-1,1}};
char s[1000001];
long long ans,x,y;
int main()
{
    int T;
    scanf("%d\n",&T);
    while (T--)
    {
        scanf("%s",s);
        ans=0,x=0,y=0;
        int l=strlen(s);
        if (l<3) {printf("0\n"); continue;}
        for (int i=0;i<l-1;++i)
        {
            long long x1=x+mov[s[i]-'0'][0];
            long long y1=y+mov[s[i]-'0'][1];
            ans+=y*x1-x*y1;
            x=x1;y=y1;
        }
        if(ans<0) ans=-ans;
        if(ans&1) printf("%lld.5\n",ans/2);
        else printf("%lld\n",ans/2);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值