计算几何--POJ--1654--Area

来源:互联网 发布:犀牛软件 下载 编辑:程序博客网 时间:2024/06/08 06:57

Description

You are going tocompute the area of a special kind of polygon. One vertex of the polygon is theorigin of the orthogonal coordinate system. From this vertex, you may go stepby step to the following vertexes of the polygon until back to the initialvertex. For each step you may go North, West, South or East with step length of1 unit, or go Northwest, Northeast, Southwest or Southeast with step length ofsquare root of 2. 

For example, this is a legal polygon to be computed and its area is 2.5: 

Input

The first line ofinput 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 describinghow the polygon is formed by walking from the origin. Here 8, 2, 6 and 4represent North, South, East and West, while 9, 7, 3 and 1 denote Northeast,Northwest, Southeast and Southwest respectively. Number 5 only appears at theend of the sequence indicating the stop of walking. You may assume that theinput polygon is valid which means that the endpoint is always the start pointand the sides of the polygon are not cross to each other.Each line may containup 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

 

 走一步算一步,不用算出来每个点,因为同向叉乘为0,不影响结果



 

#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<math.h>using namespace std;int dir[10][2]={0,0,-1,-1,0,-1,1,-1,-1,0,0,0,1,0,-1,1,0,1,1,1};int main(){char a[1000005];int t,i;cin>>t;while(t--){scanf("%s",a);int l1=strlen(a);if(l1<3){printf("0\n");continue;}long long int x=0,y=0;long long int xx,yy;long long int are=0;for(i=0;i<l1-1;i++){xx=x+dir[a[i]-'0'][0];yy=y+dir[a[i]-'0'][1];are=are+(xx*y-x*yy);x=xx;y=yy;}if(are<0)are=-are;if(are%2==0)cout<<are/2<<endl;elsecout<<are/2<<".5"<<endl;}}