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;
}

//的空间拉数据库的建立

struct Point {
   
    double x, y;
    Point(double x = 0, double y = 0) :x(x), y(y) {
   }
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) {
   
    return Vector(A.x + B.x, A.y + B.y);
}
Vector operator - (Point A, Point B) {
   
    return Vector(A.x - B.x, A.y - B.y);
}
Vector operator * (Vector A, double p) {
   
    return Vector(A.x * p, A.y * p);
}
Vector operator / (Vector A, double p) {
   
    return Vector(A.x / p, A.y / p);
}
bool operator < (const Point& a, const Point& b) {
   
    if (a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}


bool operator == (const Point& a, const Point& b) {
   
    if (sgn(a.x - b.x) == 0 && sgn(a.y - b.y) == 0)
        return true;
    return false;
}
double Dot(Vector A, Vector B) {
   
    return A.x * B.x + A.y * B.y;
}
double Length(Vector A) {
   
    return sqrt(Dot(A, A));
}
double Angle(Vector A, Vector B) {
   
    return acos(Dot(A, B) / Length(A) / Length(B));
}
double Cross(Vector A, Vector B) {
   
    return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C) {
   
    return Cross(B - A, C - B);
}
Vector Rotate(Vector A, double rad) {
   //rad为弧度 且为逆时针旋转的角
    return Vector(A.x * cos(rad) - A.y * sin(rad), A.x * sin(rad) + A.y * cos(rad));
}
Vector Normal(Vector A) {
   //向量A左转90°的单位法向量
    double L = Length(A);
    return Vector(-A.y / L, A.x / L);
}
bool ToLeftTest(Point a, Point b, Point c) {
   
    return Cross(b - a, c - b) > 0;
}

double PolygonArea(Point* p, int n) {
   //p为端点集合,n为端点个数
    double s = 0;
    for (int i = 1; i < n - 1; ++i)
        s += Cross(p[i] - p[0], p[i + 1] - p[0]);
    return s / 2;
}

char str[Max];
int main()
{
   
    FAST;
    int t;cin >> t;
    while (t--)
    {
   
        char tt;
        Point a, b, c;
        cin >> tt;int g = 0;str[0] = tt;
        while (tt != '5') {
    cin >> tt;str[++g] = tt; }
        if (g < 3)cout << 0 << endl;
        else
        {
   
            double ans = 0;
            double x = 0, y = 0;
            Point a, b, c;
            a.x = 0, a.y = 0;
            for (int i = 0;i < g;i++)
            {
   
                
                if (str[i] == '5') break;
                if (str[i] == '8') y += 1;
                else if (str[i] == '2')y -= 1;
                else if (str[i] == '6')x += 1;
                else if (str[i] == '4')x -= 1;
                else if (str[i] == '9') {
    x++;y++; }
                else if (str[i] == '7') {
    x--;y++; }
                else if (str[i] == '3') {
    x++;y--; }
                else if (str[i] == '1') {
    x--;y--; }
                if (i == 0)b.x = x, b.y = y;
                else
                {
   
                    c.x = x, c.y = y;
                    ans += Cross(b - a, c - a);
                    b = c;
                }
            }
            ans = abs(ans) / 2;
            if (ans - ll(ans) < eps) cout << (ll)ans<<endl;
            else cout<<ans<<endl;
        }
    }
}
全部评论
哇,很详细嘛
点赞 回复
分享
发布于 2022-10-24 17:53 陕西

相关推荐

点赞 评论 收藏
转发
点赞 1 评论
分享
牛客网
牛客企业服务