码迷,mamicode.com
首页 > 其他好文 > 详细

poj 1654 Area(求多边形面积 && 处理误差)

时间:2015-08-17 19:12:57      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

Area
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16894   Accepted: 4698

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

Source

题意:t组测试,每组以5结束,从原点出发,1代表向左下走一个单位,2代表向下走一个单位,3代表右下,4代表左,6代表右,7代表左上,8代表上,9代表右上,问最后围成的多边形的面积,具体如何输出看样例。

入门题~也是一道处理误差的题~就是最后判断一下叉乘和结果是奇数还是偶数,因为之前没有除以2~另外就是不能用开100万的结构体,会超内存。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <math.h>
 7 #include <algorithm>
 8 #include <cctype>
 9 #include <string>
10 #include <map>
11 #include <set>
12 #define ll long long
13 using namespace std;
14 const double eps = 1e-8;
15 struct Point
16 {
17     __int64 x,y;
18     Point() {}
19     Point(__int64 _x,__int64 _y)
20     {
21         x = _x;
22         y = _y;
23     }
24     Point operator -(const Point &b)const
25     {
26         return Point(x - b.x,y - b.y);
27     }
28     __int64 operator ^(const Point &b)const
29     {
30         return x*b.y - y*b.x;
31     }
32     __int64 operator *(const Point &b)const
33     {
34         return x*b.x + y*b.y;
35     }
36 };
37 
38 
39 int dir[10][2] = {  {0,0},{-1,-1},{0,-1},{1,-1},{-1,0}, {0,0}, {1,0},{-1,1},{0,1},{1,1}  };
40 
41 int main(void)
42 {
43     int t;
44     scanf("%d",&t);
45     while(t--)
46     {
47         int subdir;
48         Point p1,p2;
49         p1.x = p1.y = 0;
50         __int64 res = 0;
51         while(scanf("%1d",&subdir),subdir != 5)
52         {
53             p2.x = p1.x + dir[subdir][0];
54             p2.y = p1.y + dir[subdir][1];
55             res += (p1^p2);
56             p1.x = p2.x;
57             p1.y = p2.y;
58         }
59         if(res < 0)
60             res = 0 - res;
61         if(res % 2) printf("%I64d.5\n",res/2);
62         else printf("%I64d\n",res/2);
63     }
64     return 0;
65 }

 

poj 1654 Area(求多边形面积 && 处理误差)

标签:

原文地址:http://www.cnblogs.com/henserlinda/p/4737191.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!