题目给出了n个点,求任意几个点能围成的最小图形的面积。看到题的开始在图形的形状上犹豫了一上,忽然想到一定是三角形,因为多边形必然可继续切割成三角形。于是转换成了枚举三点,判断面积。注意共线情况。
本次用到了运算符重载
#include<cmath>
#include<iostream>
#include<cstdio>
#define MAXN 1005
using namespace std;
typedef struct node{
double x, y;
}dots;
dots coor[MAXN];
double operator *(dots p, dots q){
return (p.x*q.y-q.x*p.y)/2;
}
dots operator -(dots p, dots q){
dots tmp;
tmp.x=p.x-q.x;
tmp.y=p.y-q.y;
return tmp;
}
double esp=1e-8;
int main(){
int ncase;
cin>>ncase;
while(ncase--){
int n;cin>>n;
for(int i=0;i<n;i++){
scanf("%lf%lf", &coor[i].x, &coor[i].y);
}
double ans=-1;
dots ad, bd;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
for(int k=j+1;k<n;k++){
ad=coor[j]-coor[i];bd=coor[k]-coor[i];
double tmp=fabs(ad*bd);
if(tmp-0<=esp)continue;
if(ans==-1)ans=tmp;
else if(tmp<ans)ans=tmp;
}
}
}
if(ans==-1)cout<<"Impossible"<<endl;
else printf("%.2lf\n", ans);
}
return 0;
}