`

poj 1654 Area ----- 计算几何

 
阅读更多

Area
Time Limit:1000MS Memory Limit:10000K
Total Submissions:9899 Accepted:2800

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


前段时间一直在学java ,算法的进度 没跟上。 今天 做了 两道 计算几何的基础题。 这两天好好看看计算几何。 。

这题的思路: 用 向量的叉积 ( 有向面积 )。。 计算几何主要是 数学问题。


//Memory: 1172 KB		Time: 32 MS
//Language: G++		Result: Accepted

#include<stdio.h>
#include<string.h>
char a[1000100];
struct point
{
	int x,y;
}p1,p2;
int main()
{
	int n,i,len;
	long long int tempArea,area;
	int dir[9][2]={{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};
	while(scanf("%d%*c",&n)!=EOF)
	{
		p1.x=0;
		p1.y=0;
		while(n--)
		{
			tempArea=0;
			gets(a);
			len=strlen(a);
			for(i=0;i<len;i++)
			{
				int d=a[i]-'1';
				p2.x = p1.x + dir[d][0];
				p2.y = p1.y + dir[d][1];
				tempArea += p1.x * p2.y - p1.y * p2.x;
				p1.x=p2.x;
				p1.y=p2.y;
			}
			area=tempArea/2;
			if(area<0) area*=-1;
			printf("%lld",area);
			if(tempArea%2!=0) printf(".5");
			printf("\n");
		}
	}
	return 0;
}
Disqus
Like
Dislike



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics