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 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

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

又看了题解才A掉,记得自己曾对别人说WA不要马上看题解,一道题做两三天很正常,自己却MLE马上看题解

自己定的规则自己都不遵守

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <iostream>
 5 #include <algorithm>
 6 #include <math.h>
 7
 8 #define MAXX 1000002
 9 #define eps 1e-6
10 typedef struct
11 {
12     double x;
13     double y;
14 } point;
15
16 double crossProduct(point a,point b,point c)
17 {
18     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
19 }
20
21 bool dy(double x,double y)
22 {
23     return x>y+eps;
24 }
25 bool xy(double x,double y)
26 {
27     return x<y-eps;
28 }
29 bool dd(double x,double y)
30 {
31     return fabs(x-y)<eps;
32 }
33
34 int main()
35 {
36     int n,m,i,j,x,y;
37     scanf("%d",&n);
38     char str[MAXX];
39     int move[10][2]= {{0,0},{-1,-1},{0,-1},{1,-1},{-1,0},{0,0},{1,0},{-1,1},{0,1},{1,1}};
40     for(i=0; i<n; i++)
41     {
42         scanf("%s",str);
43         int len=strlen(str);
44         int x1=0,y1=0,x2,y2;
45         long long ans=0;
46         for(j=0; j<len-1; j++)
47         {
48             x2=x1+move[str[j]-‘0‘][0];
49             y2=y1+move[str[j]-‘0‘][1];
50             ans+=((x1*y2)-(x2*y1));
51             x1=x2;
52             y1=y2;
53         }
54         ans = ans > 0 ? ans : (-1)*ans;
55         if(ans == 0)
56             printf("0\n");
57             else if(ans % 2 == 0)
58             printf("%lld\n",ans/2);
59             else if(ans % 2 != 0)
60             printf("%lld.5\n",ans/2);
61     }
62 return 0;
63 }

poj 1654 Area (多边形求面积)

时间: 2024-08-03 07:16:04

poj 1654 Area (多边形求面积)的相关文章

poj 1654 Area(求多边形面积)

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

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()

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

zoj 1010 Area 判断线段是否相交(把线段扩充一倍后 好处理) + 多边形求面积

题目来源: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 题意:  给定n个点的, 如果这n个点不能形成多边形 以及 n < 3 时, 输出, "Impossible",  否则 输出 多边形的面积. 分析: 这题主要在 分析  n 个点 是否形成 多边形.  枚举 每条边,  看 这条边 是否与 其他 n - 3 条边 不规范相交. (当处理 其他 边时, 我们采用 扩充线段一倍) 代码如下: con

zoj 1010 (线段相交判断+多边形求面积)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=10 Area Time Limit: 2 Seconds      Memory Limit: 65536 KB      Special Judge Jerry, a middle school student, addicts himself to mathematical research. Maybe the problems he has thought are

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

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 thi

POJ 3348 Cows 凸包 求面积

LINK 题意:给出点集,求凸包的面积 思路:主要是求面积的考察,固定一个点顺序枚举两个点叉积求三角形面积和除2即可 /** @Date : 2017-07-19 16:07:11 * @FileName: POJ 3348 凸包面积 叉积.cpp * @Platform: Windows * @Author : Lweleth ([email protected]) * @Link : https://github.com/ * @Version : $Id$ */ #include <std

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

题目链接:http://poj.org/problem?id=1265 题意是:有一个机器人在矩形网格中行走,起始点是(0,0),每次移动(dx,dy)的偏移量,已知,机器人走的图形是一个多边形,求这个机器人在网格中所走的面积,还有就是分别求多边形上和多边形内部有多少个网格点: 皮克定理: 在一个多边形中.用I表示多边形内部的点数,E来表示多边形边上的点数,S表示多边形的面积. 满足:S:=I+E/2-1; 求E,一条边(x1,y1,x2,y2)上的点数(包括两个顶点)=gcd(abs(x1-x