poj1654--Area(几何求面积)

来源:互联网 发布:凯聪智云软件下载 编辑:程序博客网 时间:2024/05/01 05:49
Area
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 15577 Accepted: 4334

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

4582567256244865

Sample Output

000.52

Source

POJ Monthly--2004.05.15 Liu Rujia@POJ


这个题目注意数据量达到100万所以不能把点存起来,需要每步直接算面积

还有一个问题,由于面积最后算出来太大,需要存成__int64或者long long。如果怕0.5.注意最后面积尾数只会出来0.5或者整数。所以面积先用几何的求平行四边形,不除2.结果出来后判断%2就可以判断需要不需要尾数0.5了。

代码如下:

#include<stdio.h>#include<math.h>#include<string.h>#define MAXN 100005const double epsi=1e-10;inline intsign(const double &x);inline double sqr(const double &x);struct Point{__int64 x,y;Point(double _x=0,double _y=0):x(_x),y(_y){}Point operator +(const Point &op2) const{return Point(x+op2.x,y+op2.y);}Point operator -(const Point &op2) const{return Point(x-op2.x,y-op2.y);}double operator ^(const Point &op2) const{return x*op2.y-y*op2.x;}double operator *(const Point &op2) const{return x*op2.x+y*op2.y;}Point operator *(const double &d) const{return Point(x*d,y*d);}Point operator /(const double &d) const{return Point(x/d,y/d);}bool operator ==(const Point &op2) const{return sign(x-op2.x)==0&&sign(y-op2.y)==0;}};inline intsign(const double &x){if(x>epsi) return 1;//>0if(x<-epsi) return -1;//<0return 0;}inline double mul(const Point &p0,const Point &p1,const Point &p2){//p0p1与p0p2叉积 return (p1-p0)^(p2-p0);}inline double sqr(const double &x){return x*x;}inline double dis2(const Point &p0,const Point &p1){//p0p1平方 return sqr(p0.x-p1.x)+sqr(p0.y-p1.y);}inline double dis(const Point &p0,const Point &p1){//p0p1return sqrt(dis2(p0,p1));}Point p0,p1;int ptadd[10][2]={{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};__int64 res;int main(){int t,k;__int64 area=0;scanf("%d",&t);while(t--){k=0; area=0;p0.x=p0.y=0;//k++;getchar();char inp=getchar();while(inp!='5'){int inps=(int)(inp-'0'-1);int x = ptadd[inps][0];int y = ptadd[inps][1];p1.x=p0.x+x;p1.y=p0.y+y;area+=p0^p1;p0=p1;//k++;inp=getchar();}//for(int i=0;i<k;i++)//printf("~~%lf %lf~~\n",pt[i].x,pt[i].y);//for(int i=0;i<k-1;i++)if(sign(area)<0) area=-area;if(sign(area)==0) area=0.0;printf("%lld",area/2);if(area%2)printf(".5\n");elseprintf("\n");}return 0;}


0 0
原创粉丝点击