题意:起点是左边原点,然后可以向八个方向移动,每次移动一格(或者对角线),保证最终回到原点。求移动一圈所围成的多边形的面积。
题解:实则是要我们求一个多边形的面积,原点在该多边形上。
#include<cstdio>
#include<cmath>
#include<cstring>
#define eps 1e-8
#define zero(x) (((x)>0?(x):-(x))<eps)
struct Point { double x, y; };
int dir[12][2] = {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};
char str[1000007];
Point pre, now;
int main()
{
int t;
scanf("%d",&t);
while ( t-- )
{
double s = 0, tmp;
scanf("%s",str);
for ( int i = 0; str[i] != '5'; i++ )
{
if ( i == 0 ) { pre.x = dir[str[i]-'0'][0]; pre.y = dir[str[i]-'0'][1]; continue; }
now.x = pre.x + dir[str[i]-'0'][0];
now.y = pre.y + dir[str[i]-'0'][1];
tmp = now.x * pre.y - now.y * pre.x;
pre.x = now.x; pre.y = now.y;
s += tmp;
}
s = fabs(s) / 2;
if ( zero(s-floor(s))) printf("%.0lf\n",s);
else printf("%.1lf\n",s);
}
return 0;
}