HDU4709 Herding【三角形面积】

Herding

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3574    Accepted Submission(s): 1066


 

Problem 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

Source

2013 ACM/ICPC Asia Regional Online —— Warmup

 问题链接:HDU4709 Herding

问题描述:平面上给出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”。

解题思路:要求非零的最小面积,因此可以枚举每3个点,计算它们构成的三角形的面积,如果面积为0就跳过,不为零就更新答案。具体看程序。

AC的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;
}


猜你喜欢

转载自blog.csdn.net/SongBai1997/article/details/85012165
今日推荐