poj1654-area 计算多边形的面积

来源:互联网 发布:阿里云域名证书 编辑:程序博客网 时间:2024/05/16 14:19
Area
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20190 Accepted: 5501

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.5

2

注:题目里的长度有1000000个,所以这里我们用字符串进行处理,而且考虑到时间复杂度和空间复杂度我们这里使用叉积的方法来求得面积。

注意答案要使用long long 来存储。

ac代码如下:

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>using namespace std;char s[1000005];int tx[10]={0,-1,0,1,-1,0,1,-1,0,1};int ty[10]={0,-1,-1,-1,0,0,0,1,1,1};long long sum,x,y,px,py;int main(){int t,i,l;scanf("%d",&t);getchar();while(t--) {gets(s);l=strlen(s);if(l<3||s[0]=='5') sum=0;else{    sum = 0;x = y = 0;for(i=0; i<l; i++){if(s[i]=='5') break;px = x+tx[s[i]-'0'];py = y+ty[s[i]-'0'];sum += (px*y-py*x);x = px;y = py;}if(sum<0) sum = -sum;} if(sum%2==0) printf("%lld\n",sum/2);elseprintf("%lld.5\n",sum/2);}return 0;}

题目链接:点击打开链接http://poj.org/problem?id=1654



原创粉丝点击