Area

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20444   Accepted: 5567

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

题意:

从坐标(0, 0)开始,向 8 个方向画线段,线段的起终点均为整点,问围成的多边形面积。

总结:

将多面形面分成若干个三角形面积和,用向量求任意多边形的有向面积(包括非凸多边形)。设一三角形三点坐标:A(x1, y1), B(x2, y2), C(x3, y3),则面积的行列式形式如下:

按第三列展开:

这样求出一个三角形的有向面积,顺时针为负,逆时针为正。

如上图黄色线段围成的非凸多边形也可用此方法求面积,用此方法其面积表示为:

其中两个三角形的有向面积符号相反,即可求出此多边形真实面积(求出的有向面积要取绝对值)。

结论:

任意多变形的面积公式,其中(x1, y1), (x2, y2), (x3, y3) ... (xn, yn)为多边形的顶点,按顺(逆)时针排列:

此题代码:

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
int dx[10] = { 0,-1,0,1,-1,0,1,-1,0,1 };
int dy[10] = { 0,-1,-1,-1,0,0,0,1,1,1 };
string str;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t;
cin>>t;
while(t--)
{
cin>>str;
long long ans=0, px=0, py=0, nx=0, ny=0;
int len=str.size(); //.size()是无符号整型,有坑
for(int i=0; i<len-1; i++)
{
int t0=str[i]-'0';
px=nx+dx[t0];
py=ny+dy[t0];
ans+=(nx*py - ny*px);//向量求多边形有向面积,这里直接求两倍面积
nx=px;
ny=py;
}
if(ans<0)ans=-ans;
cout<<ans/2;
if(ans%2) cout<<".5";
cout<<endl;
}
return 0;
}

poj1654 -- Area (任意多边形面积)的更多相关文章

  1. hdu-2036求任意多边形面积

    改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  2. poj 1654 Area(多边形面积)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17456   Accepted: 4847 Description ...

  3. 求任意多边形面积 python实现

    数学解决方法: 多边形外选取一点,连接各点构成三角形,计算求和......  详细链接  http://blog.csdn.net/hemmingway/article/details/7814494 ...

  4. HDU 2036 求任意多边形面积向量叉乘

    三角形的面积可以使用向量的叉积来求: 对于 三角形的面积 等于: [(x2 - x1)*(y3 - y1)- ( y2 - y1 ) * ( x3 - x1 )  ] / 2.0 但是面积是有方向的, ...

  5. poj 1654 Area(求多边形面积 && 处理误差)

    Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16894   Accepted: 4698 Description ...

  6. poj 1654 Area(计算几何--叉积求多边形面积)

    一个简单的用叉积求任意多边形面积的题,并不难,但我却错了很多次,double的数据应该是要转化为long long,我转成了int...这里为了节省内存尽量不开数组,直接计算,我MLE了一发...,最 ...

  7. POJ1265——Area(Pick定理+多边形面积)

    Area DescriptionBeing well known for its highly innovative products, Merck would definitely be a goo ...

  8. poj 1654 Area 多边形面积

    /* poj 1654 Area 多边形面积 题目意思很简单,但是1000000的point开不了 */ #include<stdio.h> #include<math.h> ...

  9. hdu 2528:Area(计算几何,求线段与直线交点 + 求多边形面积)

    Area Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

随机推荐

  1. Redis哨兵模式(sentinel)部署

    1 主机环境 我这里使用的操作系统是centos 6.5,安装在vmware上,共三台. 主机名 IP 操作系统 用户名 安装目录 node1 192.168.1.101 centos 6.5 wxy ...

  2. JVM垃圾回收安全点Safe Point

    GC安全点(Safepoint) 程序执行时并非在所有地方都能停顿下来开始GC,只有在特定的位置才能停顿下来开始GC,这些位置称为“安全点(Safepoint) ” Safe Point的选择很重要, ...

  3. golang interface 类型学习

    接口类型变量的内存结构 动态类型 动态值 对于动态类型指的是当其他非接口类型变量赋值给接口类型变量时,接口类型变量中的动态类型就是当前非接口类型 对于动态值指的就是当其他非接口类型变量赋值给接口类型变 ...

  4. 容器云平台No.1~基于Docker及Kubernetes构建的容器云平台

    开篇 最近整理笔记,不知不觉发现关于kubernetes相关的笔记已经达99篇了,索性一起总结了.算是对这两年做容器云平台的一个总结,本文是开篇,先介绍下所有用到的组件.首先来看下架构图(实在画的太丑 ...

  5. 函数的arguments对象的使用和介绍

    arguments对象用于函数,当不知道传递了多少个实参,就可以使用arguments对象进行存储传递过来的实参(这时不需要写形参). arguments是一个内置对象,也是一个伪数组,我们可以按照数 ...

  6. 使用MATLAB 2019 App Design 工具设计一个 电子日记App

    使用MATLAB 2019 App Design 工具设计一个 电子日记App1.1 前言:由于信号与系统课程需要,因此下载了MATLAB软件,加之对新款的执着追求,通过一些渠道,下载了MATLAB ...

  7. 基于Java反射的定时任务设计

    一.使用场景 1.不需要立即执行.立即得到结果返回. 2.如果执行失败.需要有失败补偿机制. 3.和业务代码解耦,适用于不同的务场景. 4.调用接口的入参.出参 统计,方便查询. 二.执行顺序 1.业 ...

  8. pytorch和tensorflow的爱恨情仇之参数初始化

    pytorch和tensorflow的爱恨情仇之基本数据类型 pytorch和tensorflow的爱恨情仇之张量 pytorch和tensorflow的爱恨情仇之定义可训练的参数 pytorch版本 ...

  9. 050 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 12 continue语句

    050 01 Android 零基础入门 01 Java基础语法 05 Java流程控制之循环结构 12 continue语句 本文知识点:continue语句 continue语句 continue ...

  10. weblogic 安装+部署(一)

    昨天刚接触weblogic,在windows下搭建了一个weblogic,没什么技术,留个笔记. 1.首先要有jdk,添加环境变量这个没什么好说的. 2.下载weblogic:可以去官网下:http: ...