POJ1654 Area(多边形面积)

题目链接:

  http://poj.org/problem?id=1654

题目描述:

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.

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

题目大意:

  一个人会从原点出发,走一圈回到原点,给你路径,求围成面积的大小

思路:

  坑巨多……

  首先路径不需要全部记录,读一个面积加上一部分叉乘即可

  于是需要记录上一个点的坐标

  然后面积只有可能是整数或整数加二分之一

  所以用long long记录二倍面积,加快速度

  int会爆 ……要用long long

  不能用%g输出……手动判断……

  手写abs……防止CE

  卒_(:з」∠)__

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 const double EPS = 1e-10;
 6 const int N = 24;
 7
 8 typedef long long LL;
 9
10 int a[10] = { 0,-1,0,1,-1,0,1,-1,0,1 };    //记录每种操作走的方向
11 int b[10] = { 0,-1,-1,-1,0,0,0,1,1,1 };
12
13 inline LL ABS(LL n) { return n >= 0 ? n : -n; }    //手动abs
14
15 int main() {
16     int q;
17     char tmp[2];
18     scanf("%d", &q);
19     while (q--) {
20         LL x = 0, y = 0, area = 0, px = 0, py = 0;
21         int op;
22         while (1) {
23             scanf("%1s", tmp);
24             if (tmp[0] == ‘5‘)break;    //5终止
25             op = tmp[0] - ‘0‘;
26             x += a[op], y += b[op];
27             area += px*y - py*x;    //叉乘
28             px = x, py = y;            //更新上一个坐标
29         }
30         LL ans = ABS(area);
31         if (ans % 2 == 0)printf("%lld\n", ans / 2);
32         else printf("%lld.5\n", ans / 2);
33     }
34 }
时间: 2024-10-14 18:38:28

POJ1654 Area(多边形面积)的相关文章

POJ 1654 Area [多边形面积]

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19642   Accepted: 5376 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 thi

POJ 1654 Area 多边形面积 G++会WA

#include<stdio.h> #include<algorithm> #include <cstring> using namespace std; typedef long long ll; const int MAXN = 1000008; char s[MAXN]; int dx[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1}; int dy[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1}; int main()

POJ1265:Area(多边形面积公式+pick公式) 好题

题目:http://poj.org/problem?id=1265 题意: 题目解析: #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <math.h> #include <queue> #define inf 0x3f3f3f3f #define eps 1e-9 typedef long long ll

POJ1265——Area(Pick定理+多边形面积)

Area DescriptionBeing well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new research and development facility the company has installed the latest system of surveilla

poj 1654 Area(求多边形面积)

题意:从原点出发向八个方向走,所给数字串每个数字代表一个方向,终点与原点连线,求所得多边形面积: 思路:(性质)共起点的两向量叉积的一半为两向量围成三角形的面积.以此计算每条边首尾两个向量的叉积,求和,除二: #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const dou

Area - POJ 1654(求多边形面积)

题目大意:从原点开始,1-4分别代表,向右下走,向右走,向右上走,向下走,5代表回到原点,6-9代表,向上走,向左下走,向左走,向左上走.求出最后的多边形面积. 分析:这个多边形面积很明显是不规则的,可以使用向量积直接求出来面积即可. 代码如下: ------------------------------------------------------------------------------------------------------------------------------

ZOJ 1010 Area 求任意多边形面积

主要判断是否是多边形:1.n<3 : 2.非相邻两条线段不相交 #include <iostream> #include <cmath> #include <stdio.h> using namespace std; #define eps 1e-8 int sig(double x) { if(x<-eps) return -1; if(x>eps) return 1; return 0; } struct point { double x,y; }

poj1408——叉积求多边形面积

poj1408——叉积求多边形面积 Fishnet Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 1853   Accepted: 1185 Description A fisherman named Etadokah awoke in a very small island. He could see calm, beautiful and blue sea around the island. The previou

地球椭球面上多边形面积量算(C++代码)

昨天突然测试的时候发现以前产品中写的地球椭球面上面积计算的代码有点问题,于是今天就彻底修正,从QGIS中抠出代码来用C++重写了一下,新代码可以比较准确计算椭球面上多边形的面积,这个基础函数对空间量算功能中的面积量测非常重要,在这里共享出来供大家参考甚至直接拿过去用. 头文件如下: /** * @file DistanceArea.h * @brief 椭球面上计算多边形面积的接口文件 * @details * @author zxg * @date 2015年5月15日 * @version

多边形面积模板

HDU 2036 改革春风吹满地 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20033    Accepted Submission(s): 10256 Problem Description “ 改革春风吹满地, 不会AC没关系; 实在不行回老家, 还有一亩三分地. 谢谢!(乐队奏乐)”话说部分学生心态极好,每天就知道游戏,这次