0
点赞
收藏
分享

微信扫一扫

(计算几何POJ step 8.1.1.2)POJ 1654 Area(使用叉积来计算多边形面积)


/*
 * POJ_1654.cpp
 *
 *  Created on: 2013年11月16日
 *      Author: Administrator
 */
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>

using namespace std;

const double epsi = 1e-10;
const double pi = acos(-1.0);
const int maxn = 100005;

int sign(const double& x){
	if(x > epsi){
		return 1;
	}else if(x < -epsi){
		return -1;
	}

	return 0;
}


struct Point{
	long long x,y;
	Point(long long _x = 0,long long _y = 0):x(_x),y(_y){

	}

	Point operator-(const Point& op2)const{
		return Point(x - op2.x,y - op2.y);
	}

	Point operator+(const Point& op2)const{
		return  Point(x + op2.x,y + op2.y);
	}

	double operator^(const Point& op2)const{
		return x*op2.y - y*op2.x;
	}
};

int main(){
	int t;
	string s;
	scanf("%d",&t);
	while(t--){
		cin >> s;

		int i;
		Point p =Point(0,0);
		Point p1;

		long long ans = 0;
		for(i = 0 ; i < s.size() ; ++i){
			if(s[i] == '1'){
				p1 = p + Point(-1,-1);
			}else if(s[i] == '2'){
				p1 = p + Point(0,-1);
			}else if(s[i] == '3'){
				p1 = p + Point(1,-1);
			}else if(s[i] == '4'){
				p1 = p + Point(-1,0);
			}else if(s[i] == '5'){
				p1 = Point(0,0);
			}else if(s[i] == '6'){
				p1 = p + Point(1,0);
			}else if(s[i] == '7'){
				p1 = p + Point(-1,1);
			}else if(s[i] == '8'){
				p1 = p + Point(0,1);
			}else if(s[i] == '9'){
				p1 = p + Point(1,1);
			}

			ans += p^p1;
			p = p1;
		}

		/**
		 * 使用叉积来计算多边形的面积:
		 *
		 */
		if(ans < 0){
			ans = -ans;
		}
		printf("%lld",ans/2);
		if(ans%2 != 0){
			printf(".5");
		}
		printf("\n");
	}

	return 0;
}


举报

相关推荐

0 条评论