POJ 1654 Area 有向面积

题目描述: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-8

int 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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值