poj1654 Area (求多边形面积)

本文介绍了一种特殊的多边形面积计算方法,通过输入一系列方向指令来构建多边形,并计算其面积。重点在于如何在有限的空间内高效地模拟多边形的形成过程并实时累加小三角形面积。

poj1654 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

思路:多边形面积分成多个小三角形的面积之和,但题目给的空间太少,不能存储所有的点(不好套板子),所以需要在模拟的过程中不断加入小三角形的面积。另外需要注意的是答案输出时要控制下精度如果误差小于1e-6即算为相等,可输出整数(因为同一个数由不同方法计算而来,值会不同如1.5可能会为a=1.4999999 与 b=1.5000001,此时应算作相等)。

代码如下

#include<cstdio>
#include<cmath>
#include<iostream>
#include<string>
#include<map>
#include<algorithm>
#include<memory.h>
#define pii pair<int,int>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
typedef long long ll;
const int Max = 1e6 + 5;
using namespace std;

const double pi = acos(-1.0);
const double inf = 1e100;
const double eps = 1e-6;
int sgn(double d) {
   
   
    if (fabs(d) < eps)
        return 0;
    if (d > 0)
        return 1;
    return -1;
}
int dcmp(double x, double y) {
   
   
    if (fabs(x - y) < eps)
        return 0;
    if (x > y)
        return 1;
    return -
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_Rikka_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值