Herding
Time Limit:1000MS Memory Limit:32768KB
Description:
Little John is herding his father's cattles. As a lazy boy, he cannot tolerate chasing the cattles all the time to avoid unnecessary omission. Luckily, he notice that there were N trees in the meadow numbered from 1 to N, and calculated their cartesian coordinates (Xi, Yi). To herding his cattles safely, the easiest way is to connect some of the trees (with different numbers, of course) with fences, and the close region they formed would be herding area. Little John wants the area of this region to be as small as possible, and it could not be zero, of course.
Input:
The first line contains the number of test cases T( T<=25 ). Following lines are the scenarios of each test case.
The first line of each test case contains one integer N( 1<=N<=100 ). The following N lines describe the coordinates of the trees. Each of these lines will contain two float numbers Xi and Yi( -1000<=Xi, Yi<=1000 ) representing the coordinates of the corresponding tree. The coordinates of the trees will not coincide with each other.
Output:
For each test case, please output one number rounded to 2 digits after the decimal point representing the area of the smallest region. Or output "Impossible"(without quotations), if it do not exists such a region.
Sample Input:
1
4
-1.00 0.00
0.00 -3.00
2.00 0.00
2.00 2.00
Sample Output:
2.00
题意:
在一块草地上有编号为1 - N的树木,懒惰的John为了减少追赶牲畜的时间,John需要在这些树木中选择n颗树木,使其围成的农牧区面积尽可能小,并且农牧区面积不可能为0。
算法分析:
题目给出了n个点,求任意几个点能围成的最小图形的面积。因为多边形必然可继续切割成多个三角形,因此最小面积便是三角形。由于数据范围较小,可以直接枚举三角形的三点,来计算所有三角形中最小的面积。(注意共线情况不存在三角形)
代码实现:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <vector>
#include <stack>
using namespace std;
const int Max = 100000;
struct Point
{
double x, y;
Point(double x = 0, double y = 0) : x(x), y(y){ }
};
typedef Point Vector;
Vector operator + (Vector A, Vector B) {return Vector(A.x + B.x, A.y + B.y);}
Vector operator - (Vector A, Vector B) {return Vector(A.x - B.x, A.y - B.y);}
Vector operator * (Vector A, double p) {return Vector(A.x * p, A.y * p);}
Vector operator / (Vector A, double p) {return Vector(A.x / p, A.y / p);}
double Cross(Vector A, Vector B)///叉积计算
{
return A.x * B.y - A.y * B.x;
}
double Area2(Point A, Point B, Point C)///用叉积计算三角形面积
{
return Cross(B - A, C - A) / 2.0;
}
int main()
{
int t;
double ans, tmp;
Point po[105];
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
for(int i = 0; i < n; i++)
scanf("%lf %lf",&po[i].x,&po[i].y);
bool flag = false;
tmp = 0.0;
ans = 999999.0;
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++)
{
tmp = fabs(Area2(po[i],po[j],po[k]));
if(tmp < ans && tmp != 0.0)
{
ans = tmp;
flag = true;
}
}
if(flag)
printf("%.2f\n",ans);
else
printf("Impossible\n");
}
return 0;
}