HDU 4709 Herding(叉積求三角形面積)


Herding
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u



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 2.00
2.00 0.00
 

Sample Output

2.00
 



題目大意:

           john 要在一片森林裏放牛,但是 john 怕牛到處亂跑,而他有比較懶,所以他想找幾棵樹圍起來,這要他就可以吃着小菜、喝着小酒在旁邊悠哉了,現在他想讓你幫他算出圍城的圈中最小的面積是多少。

輸入:

輸入 t 表示有t組測試數據

輸入 n ,表示該組數據中有 n 棵樹

接下來 n 行分別輸入 n 課數的座標點

        輸出:

輸出能圍成的最小面積,但不能爲 0 

思路分析:

要圍成的面積最小,那麼肯定就是選三棵樹了,小於三棵樹的圍不成,大於三棵的圍成的面積肯定比三棵的面積大,所以就算三棵樹圍就行了,因此,該題到這就有兩種方式求,  一種是 : 一言不合就搜索 (⊙o⊙)…  從 n 棵樹種搜三棵樹,圍成的面積,取最小的。另一種就是:隨隨便便就暴力  (*@ο@*) ~  ,直接三個for 循環,暴力求解,記錄下來最小值。

不過在這道題中還有個坑的地方,精度問題 23333333333333,如果你只先根據點求出每條邊的長度,然後再根據海倫公式求面積,那麼恭喜你,你將面臨的是 WA,我就因爲這wa了無數次  ,頓時一臉懵逼O__O "….....    ,最後想想,應該是精度問題,可能中間計算出現精度損失了。 

好了,說了這麼多廢話,說說這道題用的公式吧,這道題用的是 由三點座標直接求三角形面積的公式。

設三角形的三個頂點座標爲   A( x1 , y1 )             B( x2 , y 2 )                     C ( x3 , y3 )

                        則 三角形的  面積   S  =   ( x2 - x1 ) * ( y3 - y1)  - ( y2 - y1 ) * ( x3 - x1 )

證明過程我就不寫了,大家可以自己回去推導證明一下


附上代碼:(搜索)

#include<stdio.h>
#include<math.h>
#include<string.h>
#define min(x,y)  x > y ? y : x  
double x[5],y[5],xx[300],yy[300],area;
void dfs(int n,int t)
{
	if(t == 4)
	{
		double area1 = (fabs((x[2]-x[1])*(y[3] - y[1]) - (y[2]-y[1])*(x[3]-x[1])))/2;
		if(area1 == 0)
		return;
		area = min(area1,area);
		return ;
	}
	int i;
	for(i = n;i >= 0;i--)
	{
		x[t] = xx[i];
		y[t] = yy[i];
		dfs(i-1,t+1);
	}
}

int main()
{
	int k;
	scanf("%d",&k);
	while(k--)
	{
		memset(x,0,sizeof(x));
		memset(xx,0,sizeof(xx));
		memset(y,0,sizeof(y));
		memset(yy,0,sizeof(yy));
		area = 99999;
		int l;
		scanf("%d",&l);
		int i;
		for(i = 0;i < l;i++)
		{
			scanf("%lf%lf",&xx[i],&yy[i]);
		}
		dfs(l-1,1);
		if(area == 99999)
		printf("Impossible\n");
		else
		printf("%.2lf\n",area);
	}
	return 0;
}


發佈了106 篇原創文章 · 獲贊 31 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章