poj 1654 Area

题目

题目链接:<a href="http://poj.org/problem?id=1654" target="_blank">pku1654</a> <br>

Area

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.

Sample Input

4
5
825
6725
6244865

Sample Output

0
0
0.5
2

翻译

摘自有道翻译

区域

描述

你要计算一种特殊的多边形的面积。一个顶点的多边形是正交坐标系的原点。从这个顶点,你可以走了一步一步的多边形的顶点后,直到回到最初的顶点。为每个步骤你可以去北、西、南、东与步长为1单位,或去西北,东北,西南或东南√2步长。
例如,这是一个合法的多边形计算,它的面积是2.5:

输入

输入的第一行是一个整数t(1 < = t < = 20),测试多边形的数量。每个下面的行包含一个字符串组成的数字1 - 9描述多边形是由从原点。8、2、6和4代表北方,南方,东方和西方,而9日7日3和1表示东北、西北、东南和西南。5号只出现在序列的末尾显示的停止行走。你可能认为输入多边形是有效的这意味着端点总是起始点和多边形的边不是彼此交叉。每一行可能含有多达1000000位数。

输出

对于每一个多边形,打印它的面积在一行。

有些人的题解说看翻译的走法设置方向走法就会无限WA,然而并不是这样。
初始化的x,y,可以反过来,没有太大影响。
这道题的面积用叉积/2求即可,比较恶心的是精度问题。
用int会溢出,要用long long,然而注意用了long long读入和输出都要与long long相符,不然就真的无限WA了

代码

#include <stdio.h>  
#include <string>  
#include <iostream>  


using namespace std;
  
int x[10]={0,-1,0,1,-1,0,1,-1,0,1};    
int y[10]={0,-1,-1,-1,0,0,0,1,1,1};  
  
long long cj(long long x1,long long y1,long long x2,long long y2)  
{  
    long long aj=((x1*y2)-(x2*y1));  
    return aj;  
}  
  
int main()  
{  
    int t;  
    string s;  
    scanf("%d\n",&t);   
    for (int j=1;j<=t;j++)  
    {  
        int c,x1,x2,y1,y2,d;
        long long ans=0;
        cin >> s ;  
        c=s.size();
        if (s[c-1]!='5'||c<2) {printf("%d\n",0);continue;}
        int i=0;
        x1=y1=0;
		while (s[i]!='5')  
        {   
            x2=x1;y2=y1;
            x2+=x[s[i]-'0'];y2+=y[s[i]-'0'];  
            ans+=cj(x1,y1,x2,y2);  
            x1=x2;y1=y2;
			i++;       
        }    
        if ((x2==0 && y2==0)||(s[d-1]!='5'))
		{
            if (ans<0) ans=-ans;  
            printf("%lld",ans/2);
            if (ans%2) printf(".5");
            printf("\n");
        } 
        else printf("%d\n",0);
    }  
    return 0;  
} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值