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 -

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

被折叠的 条评论
为什么被折叠?



