Description
You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orthogonal coordinate system. From this vertex, you may go step by step to the following vertexes of the polygon until back to the initial vertex. For each step you may go North, West, South or East with step length of 1 unit, or go Northwest, Northeast, Southwest or Southeast with step length of square root of 2.
For example, this is a legal polygon to be computed and its area is 2.5:
【题目分析】
其实就是一道拆分下来的叉积求面积的小水题目。只需要扫一遍就可以了,注意叉积相减的方式。刚开始因为写反了,结果WA了好多次。而且还要加入特判,当字符串长度小于3时要输出0。(因为要回到原点)
【代码】
#include <cstdio>
#include <cstring>
int mov[10][2]={{0,0},{1,-1},{1,0},{1,1},{0,-1},{0,0},{0,1},{-1,-1},{-1,0},{-1,1}};
char s[1000001];
long long ans,x,y;
int main()
{
int T;
scanf("%d\n",&T);
while (T--)
{
scanf("%s",s);
ans=0,x=0,y=0;
int l=strlen(s);
if (l<3) {printf("0\n"); continue;}
for (int i=0;i<l-1;++i)
{
long long x1=x+mov[s[i]-'0'][0];
long long y1=y+mov[s[i]-'0'][1];
ans+=y*x1-x*y1;
x=x1;y=y1;
}
if(ans<0) ans=-ans;
if(ans&1) printf("%lld.5\n",ans/2);
else printf("%lld\n",ans/2);
}
}