题意:一个人初始在原点,按照题目所给走法,求最后得到的矩形的面积;1~9分别表示八个方向,5表示停止。
分析:以起点为原点,每走一条路,把起点和终点分别和原点连接构成两个向量,然后用所得向量求出该三角形的面积;最后矩形的面积就是这若干个三角形的面积和。
#include<iostream>
#include<iomanip>
#include<queue>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const double eps=1e-8;
inline bool zero(double x)
{
return fabs(x)<eps;
}
inline int sgn(double x)
{
if (fabs(x)<eps) return 0;
if (x<0) return -1;
return 1;
}
struct Vector
{
double x,y;
Vector() {};
Vector(double _x,double _y):x(_x),y(_y) {};
Vector operator+(const Vector& b) const
{
return Vector(x+b.x,y+b.y);
}
Vector operator-(const Vector& b) const
{
return Vector(x-b.x,y-b.y);
}
Vector operator*(double q) const
{
return Vector(x*q,y*q);
}
};
typedef Vector Point;
inline double DotProduct(const Vector& a,const Vector& b)
{
return a.x*b.x+a.y*b.y;
}
inline double CrossProduct(const Vector& a,const Vector& b)
{
return a.x*b.y-a.y*b.x;
}
struct Segment
{
Point s,e;
Segment() {};
Segment(Point _s,Point _e):s(_s),e(_e) {};
};
bool IsIntersected(Segment l1,Segment l2)
{
return max(l1.s.x,l1.e.x) >= min(l2.s.x,l2.e.x) &&
max(l2.s.x,l2.e.x) >= min(l1.s.x,l1.e.x) &&
max(l1.s.y,l1.e.y) >= min(l2.s.y,l2.e.y) &&
max(l2.s.y,l2.e.y) >= min(l1.s.y,l1.e.y) &&
sgn(CrossProduct(l2.s-l1.s,l1.e-l1.s))*sgn(CrossProduct(l2.e-l1.s,l1.e-l1.s)) <= 0 &&
sgn(CrossProduct(l1.s-l2.s,l2.e-l2.s))*sgn(CrossProduct(l1.e-l2.s,l2.e-l2.s)) <= 0;
}
inline double Dist(Point a,Point b)
{
return sqrt(DotProduct(b-a,b-a));
}
inline bool DotsInline(Point a,Point b,Point c)
{
return zero(CrossProduct(a-c,b-c));
}
Point Intersection(Point u1,Point u2,Point v1,Point v2)
{
Point Ans=u1;
double temp=((u1.x-v1.x)*(v1.y-v2.y)-(u1.y-v1.y)*(v1.x-v2.x))/((u1.x-u2.x)*(v1.y-v2.y)-(u1.y-u2.y)*(v1.x-v2.x));
Ans.x+=(u2.x-u1.x)*temp;
Ans.y+=(u2.y-u1.y)*temp;
return Ans;
}
const int MAX=1000005;
const int Dx[10]={0,1,1,1,0,0,0,-1,-1,-1};
const int Dy[10]={0,-1,0,1,-1,0,1,-1,0,1};
int T;
char Str[MAX];
int main()
{
cin.sync_with_stdio(false);
cout<<fixed<<setprecision(2);
cin>>T;
while (T--)
{
cin>>Str;
long long Px,Py,Nx=0,Ny=0,Len=strlen(Str);
Point a,b;
long long Ans=0;
for (int i=0;i<Len-1;i++)
{
Px=Nx+Dx[Str[i]-'0'];
Py=Ny+Dy[Str[i]-'0'];
a.x=Nx,a.y=Ny,b.x=Px,b.y=Py;
Ans+=CrossProduct(a,b);
Nx=Px,Ny=Py;
}
if (Ans<0)
Ans=-Ans;
if (Ans%2==1)
cout<<Ans/2<<".5"<<endl;
else
cout<<Ans/2<<endl;
}
return 0;
}
POJ 1654 Area 计算几何基础
最新推荐文章于 2022-01-18 10:33:27 发布