POJ - 1654 Area (初入几何)

该博客介绍了一种计算从原点出发,通过特定步长和方向形成的合法多边形面积的方法。通过将多边形分割成三角形,并利用叉积计算三角形面积,最终求得整个图形的面积。题目给出了输入输出格式,并提到了解题关键在于理解叉积的性质及其在凸包问题中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.

题意:初始点在原点,现在给出8种走的方式,数字1~9每个数字表示一种走的方式。5表示回到原点。当走完所有点是会形成一个图形,最后问你这个图形的面积

这个图形必然可以分割有限个以原点为顶点的三角形,我们只要算出所有三角形的面积就可以得到答案。前几天复习了下凸包,刚好叉积就可以解决这个问题,叉积的性质大家自行百度,叉积的计算方式是行列式。

a=(x1,y1.0),b=(x2,y2,0).

a×b=i       j      k

        x1    y1   0

        x2    y2   0

=(0*y1-0*y2)*i+(0*x1-0*x2)*j+(x1*y2-x2*y1)*k

=(x1*y2-x2*y1)*k

而三角形的一个点是原点,那么另外两个点的向量就是(x1-0)(y1-0),(x2-0)(y2-0)。所以一个三角形的面积就可以是(x1*y2-x2*y1)

这题坑点还挺多的,写的时候要注意

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define ll long long
char s[1000005];
int dx[10]={0,1,1,1,0,0,0,-1,-1,-1};
int dy[10]={0,-1,0,1,-1,0,1,-1,0,1};
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s",s);
        int len=strlen(s);
        if(len<3&&s[0]=='5')
            printf("0\n");
        else
        {
            ll x=0,y=0,nx,ny,aa=0;
            for(int i=0; i<len; i++)
            {
                nx=x+dx[s[i]-'0'];
                ny=y+dy[s[i]-'0'];
                aa+=x*ny-y*nx;
                x=nx;
                y=ny;
            }
            if(aa<0)
                aa=-aa;
            if(aa%2==0)
            printf("%lld\n",aa/2);
            else
            printf("%lld.5\n",aa/2);
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值