POJ 1654 Area

本文介绍了解决POJ1654Area问题的方法,该问题要求计算一种特殊多边形的面积。多边形从坐标系原点开始,通过指定的步长和方向构建。输入包含多个测试用例,每个用例由一串数字组成,代表了构建多边形的步骤。文章提供了AC代码示例,展示了如何在内存限制下直接在主函数中计算多边形面积。

POJ 1654 Area

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:

Input

The first line of input is an integer t (1 <= t <= 20), the number of the test polygons. Each of the following lines contains a string composed of digits 1-9 describing how the polygon is formed by walking from the origin. Here 8, 2, 6 and 4 represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast, Northwest, Southeast and Southwest respectively. Number 5 only appears at the end of the sequence indicating the stop of walking. You may assume that the input polygon is valid which means that the endpoint is always the start point and the sides of the polygon are not cross to each other.Each line may contain up to 1000000 digits.

Output

For each polygon, print its area on a single line.

Sample Input

4
5
825
6725
6244865

Sample Output

0
0
0.5
2

这题比较坑的地方就是内存限制很小,所以一开始我套模板总是MLE,所以只能把计算凸多边形面积的公式直接写在主函数里,AC代码如下:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
double eps=1e-5;
int main()
{
   int t;
   cin>>t;
   while(t--){
      char s[1000005];
      scanf("%s",s);
      int len=strlen(s);
      if(len<3) {
            printf("0\n");
            continue;
      }
      double ss=0,x=0,y=0,xx=0,yy=0;
      for(int i=0;i<len;i++){
            if(s[i]=='8'){
                  xx=x;yy=y+1;
                  ss+=(x*yy-y*xx);
                  x=xx;
                  y=yy;
            }
            else if(s[i]=='2'){
                   xx=x;yy=y-1;
                   ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
             else if(s[i]=='6'){
                   xx=x+1;yy=y;
                   ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
             else if(s[i]=='4'){
                  xx=x-1;yy=y;
                  ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
             else if(s[i]=='9'){
                 xx=x+1;yy=y+1;
                 ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
             else if(s[i]=='7'){
                  xx=x-1;yy=y+1;
                  ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
             else if(s[i]=='3'){
                 xx=x+1;yy=y-1;
                 ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
            else if(s[i]=='1'){
                  xx=x-1;yy=y-1;
                  ss+=(x*yy-y*xx);
                   x=xx;
                   y=yy;
            }
            else if(s[i]=='5') break;
      }
      ss=fabs(ss)/2;
      if(ss-ll(ss)<eps) printf("%lld\n",ll(ss));
      else printf("%.1f\n",ss);
   }
   return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

旺 崽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值