HDU4709 Herding
http://acm.hdu.edu.cn/showproblem.php?pid=4709
题意
平面上给出N(1<=N<=100)个点,每个点给出的是浮点数的二位平面坐标(x,y)。现在要求的是这N个点中由最少3个点构成的图形中的最小面积。
输入
输入T(1<=T<=25)表示输入实例个数。接下来每个实例第一行是N,接下来N行每行包括两个浮点数表坐标(X,Y)(-1000.0<=X,Y<=1000.0)。
输出
输出找到的最小面积值(保留小数点后两位),如果所有的面积都为0.00,即不存在非0的面积,则输出“Impossible”。
样例输入
1
4
-1.00 0.00
0.00 -3.00
2.00 0.00
2.00 2.00
样例输出
2.00
分析
要求非零的最小面积,因此可以枚举每3个点,计算它们构成的三角形的面积,如果面积为0就跳过,不为零就更新答案。具体看程序。
C++程序
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const double EPS=1e-8;
const double INF=1e9;
const int N=105;
struct Point{
double x,y;
Point(){}
Point(double x,double y):x(x),y(y){}
//向量减法
Point operator -(const Point &a)const
{
return Point(x-a.x,y-a.y);
}
//向量叉积
double operator ^(const Point &a)const
{
return x*a.y-y*a.x;
}
};
int main()
{
Point p[N];
int t,n;
scanf("%d",&t);
while(t--)
{
double ans=INF;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
//枚举每3个点
bool flag=false;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
for(int k=j+1;k<n;k++)
{
double area=fabs((p[i]-p[j])^(p[k]-p[j]))/2;
if(area<EPS) continue;
flag=true;//标记存在面积非零的图形
ans=min(ans,area);
}
if(!flag)
printf("Impossible\n");
else
printf("%.2lf\n",ans);
}
return 0;
}