POJ 1654 Area

Area
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 9933   Accepted: 2813

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

 
计算几何求面积的题。假设初始点为(0,0),然后连续两点不断求叉积,最后的和即为所求面积。使用double可能会溢出所以用long long。判断一下奇偶然后决定是否输出.5即可。
 
用g++交Wrong Answer但是c++是Accepted
 
 1 #include <iostream>
 2 #include <string>
 3 #include <stack>
 4 #include <queue>
 5 #include <vector>
 6 #include <cstdlib>
 7 #include <cstdio>
 8 #include <cmath>
 9 #include <algorithm>
10 #include <functional>
11 #include <cstring>
12 
13 
14 int dx[10] = {0,-1,0,1,-1,0,1,-1,0,1};
15 int dy[10] = {0,-1,-1,-1,0,0,0,1,1,1};
16 
17 using namespace std;
18 
19 char s[1000005];
20 
21 double crs(pair<int,int> a,pair<int,int> b)
22 {
23     return a.first*b.second-a.second*b.first;
24 }
25 
26 int main(void)
27 {
28     int total;
29     cin >> total;
30     while(total--)
31     {
32         long long ans = 0;
33         scanf("%s",s);
34         pair<int,int> a(0,0),b(0,0);
35         int length = strlen(s)-1;
36         for(int i(0);i != length;++i)
37         {
38             b.first = a.first+dx[s[i]-'0'];
39             b.second = a.second+dy[s[i]-'0'];
40             ans += crs(a,b);
41             a = b;
42         }
43         if(ans < 0)
44             ans = -ans;
45         long long halfans = ans/2;
46         if(halfans*2 == ans)
47             cout << halfans << endl;
48         else
49             cout << halfans << ".5" << endl;
50     }
51 
52     return 0;
53 }

 

posted @ 2012-04-25 10:55  gluowei39  阅读(299)  评论(0编辑  收藏  举报