計算幾何--POJ--1654--Area

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;
		else
		cout<<are/2<<".5"<<endl;
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章