POJ 1654 Area (有向面積求多邊形面積)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;

struct Point
{
    double x,y;
    Point(){}
    Point(double _x,double _y)
    {
        x = _x;y = _y;
    }
    //向量
    Point operator -(const Point &b)const
    {
        return Point(x - b.x,y - b.y);
    }
    //叉積
    double operator ^(const Point &b)const
    {
        return x*b.y - y*b.x;
    }
    //點積
    double operator *(const Point &b)const
    {
        return x*b.x + y*b.y;
    }
    void input()
    {
        scanf("%lf%lf",&x,&y);
    }
};

const int maxn = 1100000;

char cmd[maxn];

int main()
{
    int t;

    scanf("%d", &t);
    while (t--) {
        scanf("%s", cmd);
        int len = strlen(cmd);
        Point cur(0,0), next;
        double ans = 0;
        for (int i = 0; i < len; i++) {
            if (cmd[i] == '8') {
                next.x = cur.x;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '2') {
                next.x = cur.x;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '6') {
                next.x = cur.x + 1;
                next.y = cur.y;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '4') {
                next.x = cur.x - 1;
                next.y = cur.y;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '1') {
                next.x = cur.x - 1;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '3') {
                next.x = cur.x + 1;
                next.y = cur.y - 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '7') {
                next.x = cur.x - 1;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            } else if (cmd[i] == '9') {
                next.x = cur.x + 1;
                next.y = cur.y + 1;
                ans += cur ^ next;
                cur = next;
            }
        }
        ans = fabs(ans) / 2.0;
        if (fabs(floor(ans) - ans) < 1e-8)
            printf("%.0lf\n", ans);
        else
            printf("%.1lf\n", ans);
    }

    return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章