现在的位置: 首页 > 综合 > 正文

POJ 1654 Area(差积求多边形面积)

2019年02月13日 ⁄ 综合 ⁄ 共 1002字 ⁄ 字号 评论关闭

走格子和算面积合二为一了,不过没有POJ上另外一道走格子的题恶心,名字叫宇航员,至今没ac,wa的代码还躺在我桌面上= =。

自认为这篇代码写得比较工整,突然想搞计算几何了~


AC代码:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <string>
#define MAXN 1000000

using namespace std;

//8北, 2南, 6东, 4西, 9东北, 7西北, 3东南, 1西南
const int dir[10][2] = {{0, 0}, {-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {0, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};
int t, n; //case数,命令数
long long area; //注意这里的long long
int x[MAXN], y[MAXN];
char move; 
char str[100];

inline void in()
{
        move = getchar();
        x[0] = 0;
        y[0] = 0;
        for (n = 1; move != '5' && move!= EOF; n++)
        {
                 x[n] = x[n-1] + dir[move-'0'][0];
                 y[n] = y[n-1] + dir[move-'0'][1];
                 move = getchar();
        }
        gets(str);
        n--;

        return ;
}

inline void calc()
{
        int x1, y1, x2, y2;
        for (int i = 1; i < n - 1; i++)
        {
                x1 = x[0] - x[i];
                y1 = y[0] - y[i];
                x2 = x[0] - x[i+1];
                y2 = y[0] - y[i+1];
                area += x1 * y2 - x2 * y1;
        }

        return ;
}

int main()
{
        #ifdef BellWind
                freopen(".in", "r", stdin);
        #endif // BellWind

        scanf("%d", &t);
        gets(str);

        while (t--)
        {
                area = 0;
                in();
                calc();
                if (area % 2)
                {
                        if (area > 0) printf("%lld.5\n", area/2);
                        else printf("%lld.5\n", -area/2);
                }
                else
                {
                        if (area > 0) printf("%lld\n", area/2);
                        else printf("%lld\n", -area/2);
                }
        }

        return 0;
}

抱歉!评论已关闭.