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. 云计算openstack核心组件——keystone身份认证服务(5)

    一.Keystone介绍:       keystone 是OpenStack的组件之一,用于为OpenStack家族中的其它组件成员提供统一的认证服务,包括身份验证.令牌的发放和校验.服务列表.用户 ...

  2. JMeter5.0在windows(含插件安装)

    一.jmeter下载 前提:已经安装jdk8+ jmeter下载地址:http://jmeter.apache.org/download_jmeter.cgi 有Binaries和Source版本 前 ...

  3. 谈谈 Java 中的那些“琐”事

    一.公平锁&非公平锁 是什么 公平锁:线程按照申请锁的顺序来获取锁:在并发环境中,每个线程都会被加到等待队列中,按照 FIFO 的顺序获取锁. 非公平锁:线程不按照申请锁的顺序来获取锁:一上来 ...

  4. Spring Eureka 本地Docker集群部署

    故事背景 最近因为产线使用的服务与发现服务,使用的是Spring Cloud Eureka集群部署,为了以后调试产线的问题,想在本地搭建和产线一样的环境.产线的所有服务都是基于K8s和Docker部署 ...

  5. java原生程序redis连接(连接池/长连接和短连接)选择问题

    最近遇到的连接问题我准备从重构的几个程序(redis和mysql)长连接和短连接,以及连接池和单连接等问题用几篇博客来总结下. 这个问题的具体发生在java原生程序和redis的交互中.这个问题对我最 ...

  6. Object.defineProperty和proxy

    Object.defineProperty问题 Object.defineProperty() 无法监控到数组下标的变化.vue只能通过以下几种方法来监听 pop() shift() unshift( ...

  7. P3378 堆(模板)

    P3378 [模板]堆 题目描述 给定一个数列,初始为空,请支持下面三种操作: 给定一个整数 x,请将 x 加入到数列中. 输出数列中最小的数. 删除数列中最小的数(如果有多个数最小,只删除 1 个) ...

  8. 如何用5000行JS撸一个关系型数据库

    首先声明,我不是标题党,我真的是用5000行左右的JS实现了一个轻量级的关系型数据库JSDB,核心是一个SQL编译器,支持增删改查. 源代码放到github上了:https://github.com/ ...

  9. Arduino 模拟引脚

    Arduino的模拟引脚的引用,网上不错的一篇文章 参考:http://blog.sina.com.cn/s/blog_156e62ef90102xjio.html 模拟引脚 本文是对于Arduino ...

  10. Arduino 寻找I2C地址address

    参考:http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-quick-tip-fin ...