Poj 1654 area (Polygon Area)

Source: Internet
Author: User

Link: http://poj.org/problem? Id = 1654

Area
Time limit:1000 ms   Memory limit:10000 K
Total submissions:14952   Accepted:4189

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

-= -=

After reading the question solution, I forgot to say to others that wa should not immediately look at the question solution. It would be normal to do a question for two or three days, but MLE should immediately read the question solution.

None of your own Rules comply

 1 #include <stdio.h> 2 #include <string.h> 3 #include <stdlib.h> 4 #include <iostream> 5 #include <algorithm> 6 #include <math.h> 7  8 #define MAXX 1000002 9 #define eps 1e-610 typedef struct11 {12     double x;13     double y;14 } point;15 16 double crossProduct(point a,point b,point c)17 {18     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);19 }20 21 bool dy(double x,double y)22 {23     return x>y+eps;24 }25 bool xy(double x,double y)26 {27     return x<y-eps;28 }29 bool dd(double x,double y)30 {31     return fabs(x-y)<eps;32 }33 34 int main()35 {36     int n,m,i,j,x,y;37     scanf("%d",&n);38     char str[MAXX];39     int move[10][2]= {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};40     for(i=0; i<n; i++)41     {42         scanf("%s",str);43         int len=strlen(str);44         int x1=0,y1=0,x2,y2;45         long long ans=0;46         for(j=0; j<len-1; j++)47         {48             x2=x1+move[str[j]-‘0‘][0];49             y2=y1+move[str[j]-‘0‘][1];50             ans+=((x1*y2)-(x2*y1));51             x1=x2;52             y1=y2;53         }54         ans = ans > 0 ? ans : (-1)*ans;55         if(ans == 0)56             printf("0\n");57             else if(ans % 2 == 0)58             printf("%lld\n",ans/2);59             else if(ans % 2 != 0)60             printf("%lld.5\n",ans/2);61     }62 return 0;63 }
View code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.