pku 1654 Area 叉积求多边形面积

http://poj.org/problem?id=1654Area

给定起点(0,0)然后给出1 - 4 | 6 - 9 表示走的方向,输入数据保证能够回到原点。很裸的叉积求多边形面积。这里精度控制很坑爹,只要出现小数就取整数+0.5,double不能控制,所以用long long或者__int64来控制。

#include <cstdio>
#include <cstring>
#include <iostream>
#define maxn 1000010

struct point
{
    int x,y;
}p[maxn];

char str[maxn];
int dir[10][2] = {{-1,-1},{0,-1},{1,-1},{-1,0},{1,0},{-1,1},{0,1},{1,1}};

double det(double x1,double y1,double x2,double y2)
{
    return x1*y2 - x2*y1;
}
int main()
{
    int t,i;
    __int64 ans;
   scanf("%d",&t);
    while (t--)
    {
        i = 1;
        double tx = 0,ty = 0;
        ans = 0;
        scanf("%s",str);
        int len = strlen(str) - 1;
        for (i = 0; i < len; ++i)
        {
            //printf("%c",str[i]);
            int pos = str[i] - '0';
            //注意我们初始化的方向是0-8,在这里调整
            if (pos > 5) pos -= 2;
            else pos--;
            double xx = tx + dir[pos][0];
            double yy = ty + dir[pos][1];
            //printf("%lf %lf %lf %lf\n",tx,ty,xx,yy);
            ans += (__int64)det(xx,yy,tx,ty);
            tx = xx; ty = yy;
        }
        if (ans < 0) ans = -ans;
        if (ans%2 != 0)
        printf("%I64d.5\n",ans/2);
        else
        printf("%I64d\n",ans/2);
    }
    return 0;
}

  

 

posted @ 2012-05-22 19:08  E_star  阅读(360)  评论(0编辑  收藏  举报