Area
 
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19398   Accepted: 5311

利用叉积求多边形面积,可以分解成多个三角形

利用面积公式,S=1/2*abs((x0*y1-x1*y0)+(x1*y2-x2*y1)...+(xn*yn-1-xn-1*yn)+(xn*y0-x0*yn)) ,把相邻两点和原点组成一个三角形,而总面积是这n个三角形面积的和,而三角形面积是两个相邻边向量的叉积

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. 
你要计算一种特殊的多边形的面积。一个顶点的多边形是正交坐标系的原点。从这个顶点,你可以走了一步一步的多边形的顶点后,直到回到最初的顶点。为每个步骤你可以去北、西、南、东与步长为1单位,或去西北,东北,西南或东南√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
#include<cstdio>
#define ll long long
ll n,m,t,x1,x2,y1,y2,ans;
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};
ll abs(ll a){return a<0?-a:a;}
int main()
{
scanf("%d",&t);
for(int i=0;i<t;i++)
{
x2=y2=ans=0;
char p=getchar();
while(p=getchar())
{
ll tmp=p-'0';
if(tmp==5) break;
x1=x2,y1=y2;
x2+=dx[tmp],y2+=dy[tmp];
ans+=(x1*y2-x2*y1);
}
ans=abs(ans);
if(ans%2==0) printf("%lld\n",ans/2);
else printf("%lld.5\n",ans/2);
}
}

  

poj 1654:Area 区域 ---- 叉积(求多边形面积)的更多相关文章

  1. [poj] 3907 Build Your Home || 求多边形面积

    原题 多组数据,到0为止. 每次给出按顺序的n个点(可能逆时针,可能顺时针),求多边形面积(保留整数) 多边形面积为依次每条边(向量)叉积/2的和 \(S=\sum _{i=1}^{n-1}p[i]* ...

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

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

  3. POJ 2954 /// 皮克定理+叉积求三角形面积

    题目大意: 给定三角形的三点坐标 判断在其内部包含多少个整点 题解及讲解 皮克定理 多边形面积s = 其内部整点in + 其边上整点li / 2 - 1 那么求内部整点就是 in = s + 1 - ...

  4. poj3348 Cows 凸包 叉积求多边形面积

    graham扫描法,参考yyb #include <algorithm> #include <iostream> #include <cstdio> #includ ...

  5. poj 1654 Area 多边形面积

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

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

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

  7. hdu 2036 求多边形面积 (凸、凹多边形)

    <题目链接> Problem Description “ 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)” 话说部分学生心态极好,每天就知道游戏,这次考 ...

  8. hdu 2036:改革春风吹满地(叉积求凸多边形面积)

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

  9. hdu 4709:Herding(叉积求三角形面积+枚举)

    Herding Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. ubuntu tomcat的安装与配置

    一.下载jdk 大概是tomat大部分是由java写的, 所以一开始安装tomcat必须得配置好jdk http://www.oracle.com/technetwork/java/javase/do ...

  2. bzoj 1798 维护序列seq

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1798 题解: 高级一点的线段树,加上了区间乘法运算,则需要增加一个数组mulv记录乘的因数 ...

  3. 使用cmd(黑窗口)敲命令使用远程数据库

    C:\Users\gzz>mysql -h 10.27.104.176 -u root -p mysql

  4. php各个版本的区别

    一. PHP 5.2.5.3.5.4.5.5.5.6 版本区别对比以及新功能详解 PHP5.2 以前:autoload, PDO 和 MySQLi, 类型约束 PHP5.2:JSON 支持 PHP5. ...

  5. 简单理解Hash算法的作用

    什么是Hash Hash算法,简称散列算法,也成哈希算法(英译),是将一个大文件映射成一个小串字符.与指纹一样,就是以较短的信息来保证文件的唯一性的标志,这种标志与文件的每一个字节都相关,而且难以找到 ...

  6. Linux下批量Kill多个进程的方法

    转自http://www.jb51.net/LINUXjishu/43534.html ps -ef|grep tt.py|grep -v grep|cut -c 9-15|xargs kill -9 ...

  7. POJ 1733 Parity game(带权并查集)

    题目链接:http://poj.org/problem?id=1733 题目大意:给你m条信息,每条信息告诉你区间l~r的1的个数是奇数还是偶数,如果后面出现信息跟前面矛盾则这条信息是错误的,问在第一 ...

  8. 《逐梦旅程 WINDOWS游戏编程之从零开始》笔记4——Direct3D编程基础

    第11章 Direct3D编程基础 2D游戏是贴图的艺术,3D游戏是渲染的艺术.这句话在我学过了之前的GDI编程之后,前一句算是有所体会,现在是来理解后一句的时候了. 安装DirectX SDK配置啥 ...

  9. redis之(九)redis的事务机制

    [一]什么是redis的事务 --->redis的事务是一组命令的集合. --->redis的事务是保证一组命令,要么都执行,要么都不执行.但不支持一组命令中,其中一个或多个执行失败,不支 ...

  10. SQL快速入门 ( MySQL快速入门, MySQL参考, MySQL快速回顾 )

    SQL 先说点废话,很久没发文了,整理了下自己当时入门 SQL 的笔记,无论用于入门,回顾,参考查询,应该都是有一定价值的,可以按照目录各取所需.SQL数据库有很多,MySQL是一种,本文基本都是SQ ...