题目链接:
http://poj.org/problem?id=1654
题目描述:
Area
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
题目大意:
一个人会从原点出发,走一圈回到原点,给你路径,求围成面积的大小
思路:
坑巨多……
首先路径不需要全部记录,读一个面积加上一部分叉乘即可
于是需要记录上一个点的坐标
然后面积只有可能是整数或整数加二分之一
所以用long long记录二倍面积,加快速度
int会爆 ……要用long long
不能用%g输出……手动判断……
手写abs……防止CE
卒_(:з」∠)__
代码:
1 #include <iostream> 2 #include <cstdio> 3 using namespace std; 4 5 const double EPS = 1e-10; 6 const int N = 24; 7 8 typedef long long LL; 9 10 int a[10] = { 0,-1,0,1,-1,0,1,-1,0,1 }; //记录每种操作走的方向 11 int b[10] = { 0,-1,-1,-1,0,0,0,1,1,1 }; 12 13 inline LL ABS(LL n) { return n >= 0 ? n : -n; } //手动abs 14 15 int main() { 16 int q; 17 char tmp[2]; 18 scanf("%d", &q); 19 while (q--) { 20 LL x = 0, y = 0, area = 0, px = 0, py = 0; 21 int op; 22 while (1) { 23 scanf("%1s", tmp); 24 if (tmp[0] == ‘5‘)break; //5终止 25 op = tmp[0] - ‘0‘; 26 x += a[op], y += b[op]; 27 area += px*y - py*x; //叉乘 28 px = x, py = y; //更新上一个坐标 29 } 30 LL ans = ABS(area); 31 if (ans % 2 == 0)printf("%lld\n", ans / 2); 32 else printf("%lld.5\n", ans / 2); 33 } 34 }