POJ 1654 Area
发布日期:2021-07-27 13:45:06 浏览次数:1 分类:技术文章

本文共 2154 字,大约阅读时间需要 7 分钟。

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

================================这是分割线===================================

一道waterful的叉积问题,然而还是wa了许多次。

1 需要注意是向量(有方向),所以中间不能取绝对值。
2 要用 long long。

================================这是分割线===================================

#include
#include
#include
using namespace std;long long ans;char c[1000001];int movx[10]={
0,-1,0,1,-1,0,1,-1,0,1},movy[10]={
0,-1,-1,-1,0,0,0,1,1,1};int main(){ int t; scanf("%d",&t); while(t--) { ans=0; long long x1=0,y1=0,x2=0,y2=0; scanf("%s",c); int l=strlen(c); for(int i=0;i<=l-1;i++) { x1+=movx[c[i]-'0']; y1+=movy[c[i]-'0']; ans+=x1*y2-x2*y1; x2=x1; y2=y1; } if(ans<0) ans*=-1; printf("%lld",ans/2); if(ans&1) printf(".5"); printf("\n"); }}

转载地址:https://blog.csdn.net/Rlt1296/article/details/51879325 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:POJ 2186 Popular Cows
下一篇:BZOJ 1115: [POI2009]石子游戏Kam

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2024年04月02日 08时59分18秒