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

POJ1654 Area(多边形面积)

时间:2017-06-11 10:28:35      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:problem   坐标   init   链接   exe   follow   enter   following   out   

题目链接:

  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(多边形面积)

标签:problem   坐标   init   链接   exe   follow   enter   following   out   

原文地址:http://www.cnblogs.com/hyp1231/p/6984277.html

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