POJ 1654 Area

题意:从原点出发,沿着8个方向走,每次走1个点格或者根号2个点格的距离,最终回到原点,求围住的多边形面积。

分析:直接记录所经过的点,然后计算多边形面积。注意,不用先保存所有的点,然后计算面积,边走变算,不然会超内存。最多有1000000个点。

注意:精度问题,使用long long /__int64,直接使用double不准确。方向的处理使用数组。
// Time 94ms; Memory 1036K
#include<iostream>
#include<cstring>
#define maxn 1000010

using namespace std;

char s[maxn];
long long dx[]={-1,0,1,-1,0,1,-1,0,1},dy[]={-1,-1,-1,0,0,0,1,1,1};

struct point
{
	long long x,y;
	point(long long xx=0,long long yy=0):x(xx),y(yy){}
}a,b;

long long cross()
{
	return a.x*b.y-a.y*b.x;
}
int main()
{
	int i,t,l;
	long long are;
	cin>>t;
	while(t--)
	{
		cin>>s;
		l=strlen(s);
		if(l<2)
		{
			cout<<"0"<<endl;continue;
		}
		a=point(dx[s[0]-49],dy[s[0]-49]);
		b=point(a.x+dx[s[1]-49],a.y+dy[s[1]-49]);
		are=cross();
		for(i=2;s[i];i++)
		{
			a=b;
			b=point(a.x+dx[s[i]-49],a.y+dy[s[i]-49]);
			are+=cross();
		}
		if(are<0) are=-are;
		cout<<are/2;
		if(are%2) cout<<".5";
		cout<<endl;
	}
	return 0;
}


原文地址:https://www.cnblogs.com/jiangu66/p/3215149.html