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;
}

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

被折叠的 条评论
为什么被折叠?



