POJ 1654 Area 有向面积

来源:互联网 发布:手机视频制作软件 编辑:程序博客网 时间:2024/05/17 12:02

题目描述:http://poj.org/problem?id=1654

解题思路:

利用叉积的性质计算三角形面积然后相加。多边形不要求一定是凸多边形,因为叉积计算出的是有向面积,带有正负号,凹下的部分会自动减去。


/* * 2014.11.10 * Problem: 1654 * Memory: 932K<span style="white-space:pre"></span>Time: 63MS * Language: C++Result: Accepted * */ #include "iostream"#include "algorithm"#include "cmath"#define EPS 1e-8int moveY[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};int moveX[] = {-1, -1, -1, 0, 0, 0, 1, 1, 1};char step[1000007];int main() {int t; scanf("%d\n", &t);long long  x, y, px, py;double area;long pos = 0;while (t--) {x = y = px = py = 0;pos = area = 0;scanf("%s", step); // 一次性读入速度更快while (step[pos] != '5') {x = px + moveX[step[pos]-'1'];y = py + moveY[step[pos]-'1'];area += 0.5*(px*y-py*x);px = x;py = y;pos++;}area = fabs(area);if (fabs((long long)area - area)<EPS) {printf("%.0lf\n", area);} else {printf("%.1lf\n", area);}}return 0;}


0 0
原创粉丝点击