[原创]POJ 1654 Area 【叉乘+外积的几何意义】【计算几何】
[原创]POJ 1654 Area 【叉乘+外积的几何意义】【计算几何】
2016-04-01 13:54:06 Tabris_ 阅读数:795
博客爬取于2020-06-14 22:44:43
以下为正文
版权声明:本文为Tabris原创文章,未经博主允许不得私自转载。
https://blog.csdn.net/qq_33184171/article/details/51036352
题目链接—> 点此传送阵
Area
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17766 Accepted: 4925
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) 给你一个字符串,其中1,2,3,4,6,7,8,9分别对应向8个方向 最后走回来 确保 数据最后能走回原点 问行走路线所圈成的多边形面积。。
最开是就想到了要用外积的几何意义来做 但是题目不能确保所走过的路线能是一个凸包 所以不敢用 然后就一直没有别的思路 最后按照样例 画了画 发现 外积的几何意义成立 于是有画了个凹包 发现也成立 于是敲了敲代码 就Ac了。。
外积的几何意义:
外积也称叉积或向量积
几何意义就是两个向量所构成的平行四边形面积就是外积
三角形的话就是外积的一半
如上图所示
Sabcd=αxβ;
SΔabc=αxβ;
本题就是把除原点外的相邻两个点与原点组成的三角形的面积加和就行
用外积求面积是会遇到结果为负的情况 是正常的 ,这样最后得到的和就会把多加上去的消掉 ,
注意 最终结果为正
注意 double的精度不好控制 用long long int 比较好控制
附本题代码
1 | # include <iostream> |