poj 1654 Area(求多边形面积 && 处理误差)

Area

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 16894   Accepted: 4698

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

POJ Monthly--2004.05.15 Liu [email protected]

题意:t组测试,每组以5结束,从原点出发,1代表向左下走一个单位,2代表向下走一个单位,3代表右下,4代表左,6代表右,7代表左上,8代表上,9代表右上,问最后围成的多边形的面积,具体如何输出看样例。

入门题~也是一道处理误差的题~就是最后判断一下叉乘和结果是奇数还是偶数,因为之前没有除以2~另外就是不能用开100万的结构体,会超内存。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <math.h>
 7 #include <algorithm>
 8 #include <cctype>
 9 #include <string>
10 #include <map>
11 #include <set>
12 #define ll long long
13 using namespace std;
14 const double eps = 1e-8;
15 struct Point
16 {
17     __int64 x,y;
18     Point() {}
19     Point(__int64 _x,__int64 _y)
20     {
21         x = _x;
22         y = _y;
23     }
24     Point operator -(const Point &b)const
25     {
26         return Point(x - b.x,y - b.y);
27     }
28     __int64 operator ^(const Point &b)const
29     {
30         return x*b.y - y*b.x;
31     }
32     __int64 operator *(const Point &b)const
33     {
34         return x*b.x + y*b.y;
35     }
36 };
37
38
39 int dir[10][2] = {  {0,0},{-1,-1},{0,-1},{1,-1},{-1,0}, {0,0}, {1,0},{-1,1},{0,1},{1,1}  };
40
41 int main(void)
42 {
43     int t;
44     scanf("%d",&t);
45     while(t--)
46     {
47         int subdir;
48         Point p1,p2;
49         p1.x = p1.y = 0;
50         __int64 res = 0;
51         while(scanf("%1d",&subdir),subdir != 5)
52         {
53             p2.x = p1.x + dir[subdir][0];
54             p2.y = p1.y + dir[subdir][1];
55             res += (p1^p2);
56             p1.x = p2.x;
57             p1.y = p2.y;
58         }
59         if(res < 0)
60             res = 0 - res;
61         if(res % 2) printf("%I64d.5\n",res/2);
62         else printf("%I64d\n",res/2);
63     }
64     return 0;
65 }
时间: 2024-08-06 10:13:11

poj 1654 Area(求多边形面积 && 处理误差)的相关文章

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

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

poj 1654 Area(多边形面积)

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17456   Accepted: 4847 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 (多边形求面积)

链接:http://poj.org/problem?id=1654 Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14952   Accepted: 4189 Description You are going to compute the area of a special kind of polygon. One vertex of the polygon is the origin of the orth

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

poj1265--Area(求多边形面积+匹克定理)

Area Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5150   Accepted: 2308 Description Being well known for its highly innovative products, Merck would definitely be a good target for industrial espionage. To protect its brand-new resear

hdu 2036 求多边形面积 (凸、凹多边形)

<题目链接> Problem Description " 改革春风吹满地,不会AC没关系;实在不行回老家,还有一亩三分地.谢谢!(乐队奏乐)" 话说部分学生心态极好,每天就知道游戏,这次考试如此简单的题目,也是云里雾里,而且,还竟然来这么几句打油诗.好呀,老师的责任就是帮你解决问题,既然想种田,那就分你一块.这块田位于浙江省温州市苍南县灵溪镇林家铺子村,多边形形状的一块地,原本是linle 的,现在就准备送给你了.不过,任何事情都没有那么简单,你必须首先告诉我这块地到底有多

三角剖分求多边形面积的交 HDU3060

1 //三角剖分求多边形面积的交 HDU3060 2 3 #include <iostream> 4 #include <cstdio> 5 #include <cstring> 6 #include <stack> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std; 11 12 const int max

poj 1654 Area(求多边形面积)

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

POJ 3348 Cows(凸包+多边形面积)

先求出凸包,然后利用凸包求出面积,除以50就是答案 代码: #include<cstdio> #include<cmath> #include<algorithm> using namespace std; const int MAXN=10005; struct Point { double x, y; Point() {} Point(double x, double y) { this->x = x; this->y = y; } void read(