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. Docker实战(2):主从库搭建

    入门 基于Docker的Mysql主从复制搭建 首先安装docker 拉取mysql镜像:5.7版本 启动主从数据库容器 docker run -p 3339:3306 --name Maste -e ...

  2. Jenkins+Git+Gitlab+Ansible实现持续集成自动化部署动态网站(7)

    项目前言 在上一篇博客<Jenkins+Git+Gitlab+Ansible实现持续化集成一键部署静态网站(一)–技术流ken>中已经详细讲解了如何使用这四个工具来持续集成自动化部署一个静 ...

  3. redis连接池参数动态化

    有的时候要从后端获取数据,真实的key可能在参数之上做一些修改,查了下set-misc模块,set_unescape_uri命令支持变量替换 location ~ /get_redis$ {      ...

  4. MaaS系统概述

    摘要:共享经济正改变着人们的生活方式,城市公共交通系统应该顺应共享经济的潮流进行转型.近年来,西方国家提出的“出行即服务(MaaS)”理念为我国解决日益严重的城市交通拥堵问题提供了新的思路.基于Maa ...

  5. Redis学习(五)Redis知识点总结

    一.基础概念 Q:什么是 Redis? 定义:Redis 是完全开源免费基于内存亦可持久化的,遵守 BSD 协议,是一个高性能的 key-value 数据库. 特点: 数据的持久化 :可以将内存中的数 ...

  6. SQLSERVER如何在子查询中使用ORDER BY

    今天在使用公司的一个pager接口的时候,需要传递一个查询的SQL语句,因为我希望他能够在pager对他查询出来的结果排序之前自己先进行排序, 于是在这个SQL中添加了ORDER BY,但是得到的结果 ...

  7. mysql-15-view

    #视图 /* 含义:虚拟表,和普通表一样使用.通过表动态生成的数据 只保存了sql逻辑,不保存查询结果 应用场景: 1.多个地方用到同样的查询结果 2.该查询结果使用的sql语句较为复杂 */ USE ...

  8. 【随笔】菜刀(代码执行)函数和命令执行函数详解及Getshell方法

    代码执行函数 VS 命令执行函数 一直想整理这两块的内容,但是一直没时间弄,直到前两天碰上一个写入了菜刀马但是死活连不上菜刀的站,顿时不知道怎么继续了,所以就趁这个机会整理了一下代码执行函数怎么get ...

  9. Camera光学、成像和 3A 算法 (视觉),camera开发

    简单介绍 转载:https://blog.csdn.net/ShareUs/article/details/94295628 成像与光学.计算机视觉,图像处理,数字成像.自动驾驶与视觉. 镜头设计:人 ...

  10. C 清空输入缓冲区,以及fflush(stdin)的使用误区和解决方法

    转载:https://blog.csdn.net/Veniversum/article/details/62048870 对C 语言初学者来说,fflush(stdin)函数被解释为会清空输入缓冲区的 ...