zoukankan      html  css  js  c++  java
  • POJ1065 Area

    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 18499   Accepted: 5094

    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

    Source

     
     
    最终会回到原点,所以只需要模拟每一步行走,用叉积计算当前点、上一个点、原点构成的三角形面积,累计求和即可。
     
    注意:1、需要long long
       2、单串长度可能小于3,需要特判输出0(加上这个就对了,诡异。大概是会出现步数不够,走不回原点的情况?)
     
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #include<cmath>
     6 using namespace std;
     7 int mx[10]={0,1,1,1,0,0,0,-1,-1,-1},
     8     my[10]={0,-1,0,1,-1,0,1,-1,0,1}; 
     9 long long s;
    10 int x,y;
    11 long long area(int x1,int y1,int x2,int y2){//叉积求面积 
    12     return x1*y2-x2*y1;
    13     
    14 }
    15 int main(){
    16     int T;
    17     scanf("%d
    ",&T);
    18     while(T--){
    19         char str[1000010];
    20         scanf("%s",str);
    21         s=0;
    22         x=0;y=0;
    23         int len=strlen(str);
    24         //
    25         if(len<3){
    26             printf("0
    ");
    27             continue;
    28         }
    29         //
    30         for(int i=0;i<len;i++){
    31             int num=str[i]-'0';
    32             int nowx=x+mx[num];
    33             int nowy=y+my[num];
    34             s+=area(x,y,nowx,nowy);
    35             x=nowx;
    36             y=nowy;
    37         }
    38         if(s<0)s=-s;
    39         if(s&1){
    40             printf("%lld.5
    ",s/2);
    41         }
    42         else printf("%lld
    ",s/2);
    43     }
    44     return 0;
    45 }
  • 相关阅读:
    Cocos游戏引擎,让小保安成就大梦想
    Android 高仿QQ5.2双向側滑菜单DrawerLayout实现源代码
    Java中字符串相等与大小比較
    Android四大基本组件之 Activity
    C++基础学习教程(五)
    HAWQ技术解析(八) —— 大表分区
    Jenkins 安装与使用--实例
    Android多点触控技术,实现对图片的放大缩小平移,惯性滑动等功能
    Mycat(4):消息表mysql数据库分表实践
    谋哥:《App自推广》开篇之回到远古人类
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5657320.html
Copyright © 2011-2022 走看看