1. 程式人生 > >POJ1654 Area(多邊形面積)

POJ1654 Area(多邊形面積)

while appears problem string namespace until ret his sequence

題目鏈接:

  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 }

POJ1654 Area(多邊形面積)