Question: Give the coordinates of N points, take some points from them to form a polygon, and find the minimum area of the polygon, output "Impossible" (number of test groups T <= 25, 1 <= N <= 100,-1000 <= coordinates Xi, Yi <= 1000 ). --> Minimum area. If yes, it must be a triangle. Determine whether a triangle can be formed at three points. If the slope is used, it is troublesome and there may be precision errors. Use the cross product to determine if the cross product of the two vectors is 0 ). Note: N can be 1 or 2, and a triangle cannot be determined.
#include <cstdio> #include <cmath> #include <algorithm> using namespace std; const int maxn = 100 + 10; const double eps = 1e-10; const double INF = 1 << 30; int N; struct Point{ double x; double y; Point(double x = 0, double y = 0):x(x), y(y){} }p[maxn]; typedef Point Vector; Vector operator + (Point A, Point B){ return Vector(A.x + B.x, A.y + B.y); } Vector operator - (Point A, Point B){ return Vector(A.x - B.x, A.y - B.y); } Vector operator * (Point A, double p){ return Vector(A.x * p, A.y * p); } Vector operator / (Point A, double p){ return Vector(A.x / p, A.y / p); } double Cross(Vector A, Vector B){ return A.x * B.y - B.x * A.y; } double Area2(Point A, Point B, Point C){ return Cross(B-A, C-A); } int dcmp(double x){ if(fabs(x) < eps) return 0; else return x < 0 ? -1 : 1; } void read(){ scanf("%d", &N); for(int i = 0; i < N; i++) scanf("%lf%lf", &p[i].x, &p[i].y); } void solve(){ double Min = INF; if(N >= 3){ for(int i = 0; i < N; i++) for(int j = i+1; j < N; j++) for(int k = j+1; k < N; k++) if(dcmp(Cross(p[j] - p[i], p[k] - p[i]))){ double temp = fabs(Area2(p[i], p[j], p[k])); Min = min(Min, temp); } } if(dcmp(Min - INF) == 0) puts("Impossible"); else printf("%.2f\n", Min/2); } int main() { int T; scanf("%d", &T); while(T--){ read(); solve(); } return 0; }