POJ 1654 Area

来源:互联网 发布:怎么进入淘宝分销平台 编辑:程序博客网 时间:2024/06/06 19:19
Area
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 14055 Accepted: 3961

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//此题还有凹多边形之分,计算的时候不能加绝对值!!!
#include <stdio.h>#include <string.h>#include <math.h>char a[1000005];struct point{    int x;    int y;}b[1000005];int main (void){    //freopen("1654.txt","r",stdin);    __int64 l,n,i,j,ans;    double sum;    while(scanf("%I64d",&n)==1)    {        for(i=1;i<=n;i++)        {            b[0].x=0;            b[0].y=0;            scanf("%s",a);            l=strlen(a);            if(a[0]=='5')                {                    printf("0\n");                    continue;                }            else            {                for(j=0;j<l-1;j++)                {                    if(a[j]=='1') {b[j+1].x=b[j].x-1;b[j+1].y=b[j].y-1;}                    else if(a[j]=='2') {b[j+1].x=b[j].x;b[j+1].y=b[j].y-1;}                    else if(a[j]=='3') {b[j+1].x=b[j].x+1;b[j+1].y=b[j].y-1;}                    else if(a[j]=='4') {b[j+1].x=b[j].x-1;b[j+1].y=b[j].y;}                    else if(a[j]=='6') {b[j+1].x=b[j].x+1;b[j+1].y=b[j].y;}                    else if(a[j]=='7') {b[j+1].x=b[j].x-1;b[j+1].y=b[j].y+1;}                    else if(a[j]=='8') {b[j+1].x=b[j].x;b[j+1].y=b[j].y+1;}                    else {b[j+1].x=b[j].x+1;b[j+1].y=b[j].y+1;}                }                //for(j=0;j<l;j++)                 //   printf("%d %d\n",b[j].x,b[j].y);            sum=0;            for(j=1;j<l-1;j++)                    sum+=1.0*b[j].x*b[j+1].y-1.0*b[j+1].x*b[j].y;                   // printf("%d\n",sum);                   ans=(__int64)sum;                   // printf("%lf\n",sum);                   if(ans<0)                    ans=-ans;                    if(ans%2==0)                       printf("%I64d\n",ans/2);                    else                       printf("%I64d.5\n",ans/2);            }        }    }    return 0;}


0 0
原创粉丝点击