這個題本身也確實比較簡單,題目說求最小面積,要面積最小的話,自然是三角形。所以問題就轉化在已知點中,求最小的三角形面積。
計算三角形面積,可以用海倫公式,也可以使用叉乘。我在這裏使用的是叉乘。
做題的過程中,我也是遇到了兩個問題。一個是在點的定義上用float的話會WA。所以還是使用double 來定義點。另外就是一定要判斷三個點是否能夠構成三角形。
下面是AC代碼:
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
#define fn(i,n) for(int i = 0; i < n; i++)
const double EPS = 1e-8;
const int INF = 0x3f3f3f3f;
struct piont
{
double x, y;
} p[111];
double area(piont a,piont b,piont c)
{
return fabs((b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y))/2.0;
}
int main()
{
int T;
cin >> T;
while(T--)
{
int n , sign = 1;
double ans = INF;
cin >> n;
fn(i,n) cin >> p[i].x >> p[i].y;
for(int i = 0; i < n - 2; i++)
for(int j = i + 1; j < n - 1; j++)
for(int k = j + 1; k < n; k++)
{
double tmp;
tmp = area(p[i],p[j],p[k]);
if((tmp < ans)&&(tmp > EPS)) {
sign = 0;
ans = tmp;
}
}
if(sign)cout << "Impossible" << endl;
else printf("%.2f\n", ans);
}
return 0;
}