poj1654

来源:互联网 发布:windows batch script 编辑:程序博客网 时间:2024/05/17 06:41

题意:从直角坐标系的原点,移动n步,每一步,可以向八个方向移动(九宫格中间向周围的八个方向移动),最终会形成一个规范的多边形(可凸可凹),求多边形面积,

简单利用叉积求多边形面积即可(有向面积的利用),注意要用__int64

#include <iostream>#include <cstdio>#include <cstring>using namespace std;int dir[10][2] = {{0, 0}, {-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {0, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}};char str[1000005];void func(char str[], int& i, __int64& x, __int64& y) {    while (str[i] != '5' && str[i] == str[i-1]) {        x += dir[str[i]-'0'][0];        y += dir[str[i]-'0'][1];        i++;    }}int main(){    int n, i;    scanf ("%d", &n);    while (n--) {       scanf ("%s", str);       if (str[0] == '5') {          printf ("0\n"); continue;       }       __int64 x1 = dir[str[0]-'0'][0], y1 = dir[str[0]-'0'][1], x2, y2;       __int64 area = 0;       i = 1;       func(str, i, x1, y1);       for (; str[i] != '5';) {           x2 = x1 + dir[str[i]-'0'][0];           y2 = y1 + dir[str[i]-'0'][1];           i++;           func(str, i, x2, y2);           area += x1 * y2 - x2 * y1;           x1 = x2;           y1 = y2;       }       if (area < 0) area = -area;       printf ("%I64d", area/2);       if (area % 2) printf (".5");       printf ("\n");    }    return 0;}




原创粉丝点击