Area
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
POJ Monthly--2004.05.15 Liu Rujia@POJ
题目类型:多边形面积
算法分析:由于这道题没有SPJ,所以要使用long long来运算,否则使用double会出现精度误差!!!注意最后输出的时候若结果是整数就输出整数,否则输出小数!!!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
/************************************************* Author :supermaker Created Time :2016/1/28 11:12:15 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e6 + 66; const int dx[] = {-6, 1, 1, 1, 0, -6, 0, -1, -1, -1}; const int dy[] = {-6, -1, 0, 1, -1, -6, 1, -1, 0, 1}; struct Point { LL x, y; Point () {} Point (LL xx, LL yy) : x (xx), y (yy) {} Point operator - (const Point &a) const { return Point (x - a.x, y - a.y); } LL operator ^ (const Point &a) const { return x * a.y - y * a.x; } LL operator * (const Point &a) const { return x * a.x + y * a.y; } }; char s[maxn]; int n; int main() { //CFF; //CPPFF; int t; scanf ("%d", &t); while (t--) { scanf (" %s", s); n = strlen (s); LL px = 0, py = 0; Point p1 = Point (px, py), p2; LL res = 0; for (int i = 0; i < n - 1; i++) { int pos = s[i] - '0'; px = px + dx[pos], py = py + dy[pos]; p2 = Point (px, py); res += (p1 ^ p2); p1 = p2; } if (res < 0) res = -res; //res = abs (res); if (res & 1) printf ("%lld.5\n", res / 2); else printf ("%lld\n", res / 2); } return 0; } |
- « 上一篇:poj3348
- poj1265:下一篇 »