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

  1. 4
  2. 5
  3. 825
  4. 6725
  5. 6244865

Sample Output

  1. 0
  2. 0
  3. 0.5
  4. 2

题意:

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

总结:

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

按第三列展开:

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

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

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

结论:

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

此题代码:

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cmath>
  4. using namespace std;
  5. int dx[10] = { 0,-1,0,1,-1,0,1,-1,0,1 };
  6. int dy[10] = { 0,-1,-1,-1,0,0,0,1,1,1 };
  7. string str;
  8. int main()
  9. {
  10. ios::sync_with_stdio(false);
  11. cin.tie(0);
  12. cout.tie(0);
  13. int t;
  14. cin>>t;
  15. while(t--)
  16. {
  17. cin>>str;
  18. long long ans=0, px=0, py=0, nx=0, ny=0;
  19. int len=str.size(); //.size()是无符号整型,有坑
  20. for(int i=0; i<len-1; i++)
  21. {
  22. int t0=str[i]-'0';
  23. px=nx+dx[t0];
  24. py=ny+dy[t0];
  25. ans+=(nx*py - ny*px);//向量求多边形有向面积,这里直接求两倍面积
  26. nx=px;
  27. ny=py;
  28. }
  29. if(ans<0)ans=-ans;
  30. cout<<ans/2;
  31. if(ans%2) cout<<".5";
  32. cout<<endl;
  33. }
  34. return 0;
  35. }

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. C语言01

    从问题到C语言程序设计 1.1计算机的问题求解方法 程序设计面向的问题 什么问题可以用程序的方法解决? 打印九九乘法表 图形变换 文件压缩问题 ....... 一切可计算的问题 如何解决? 确定问题可 ...

  2. Prometheus-Alertmanager告警对接到企业微信

    之前写过将Prometheus的监控告警信息通过Alertmanager推送到钉钉群. 最近转移了阵地,需要将Prometheus监控告警信息推送到企业微信群,经过两天的摸索,以及查了网上的一些资料, ...

  3. Spring Cloud Alibaba生态探索:Dubbo、Nacos及Sentinel的完美结合

    @ 目录 背景 一.项目框架 1.1 采用IDEA和Maven多模块进行项目搭建 1.2 模块管理及版本管理 二.微服务公共接口 2.1 定义一个公共接口Api 2.2 pom.xml 2.3 Goo ...

  4. 分享一些好用的 Chrome 插件!

    使用浏览器扩展程序可以使你的工作效率提高数倍不止,那么下面我就向大家分享一下我日常使用的扩展,可能大多数扩展大家都已经在使用了,不过也难免有一两个是你不知道的. 以下排名并不分先后,请坚持看到最后,或 ...

  5. C#类型与变量

    C#入门笔记 8.28开始看刘铁猛的视频,到9.22看完.大概觉得自己入门了,对OOP也有一定了解了,稍微写点笔记,当复习了. 类型与变量 数据类型 数据类型[1]是数据在内存中存储时的"型 ...

  6. VMware虚拟机ubuntu下安装VMware Tools步骤

    双击VMware Tools进入 找到后缀.tar.gz的压缩文件 将压缩文件复制到home目录下,home目录即左侧的主目录文件夹 打开命令行终端,默认应该就是home目录,如果不是home目录,在 ...

  7. .NETCore在析构函数(Finalize)在Linux下引起程序异常退出现象

    目       录 1.      现象概述... 1 2.      操作数据库的代码... 2 3.      引起的异常... 2 4.      异常信息分析... 3 5.      分析结 ...

  8. JAVA简单上传图片至七牛

    utils package com.example.demo.utils; import com.alibaba.fastjson.JSONObject; import com.qiniu.commo ...

  9. Oracle 11G RAC11.2.0.4 + Redhat7.3安装手册

    安装思路: 1.安装两台redhat7 linux系统 2.网络配置(双网卡,public,vip,private,scan) 3.存储配置(内存配置,ASM共享存储:6块5G共享盘udev,根目录留 ...

  10. Python练习题 035:Project Euler 007:第10001个素数

    本题来自 Project Euler 第7题:https://projecteuler.net/problem=7 # Project Euler: Problem 7: 10001st prime ...