2020 BIT冬训-模拟与暴力 H - Herding HDU - 4709
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.
InputThe 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.OutputFor 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
这题只要暴力枚举就好了。唯一的问题点在于计算三角形面积上。
由于已经在坐标系上且给了我们坐标了。直接用两向量叉乘除以二计算即可。
S = fabs(x1*y2+x2*y3+x3*y1-(x3*y2+x2*y1+x1*y3))/2
= fabs(( x2 - x1 ) * ( y3 - y1) - ( y2 - y1 ) * ( x3 - x1 ))/2;//也即是BA,CA向量的叉乘(absin<a,b>)再除以2
不过还要判断一下是否能构成三角形(即面积是否大于0)
AC代码如下
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; int t,n; double ans=0xffffffff,a,b,c,d,s; struct tree{ double x,y,disx,disy; }tr[105]; int main(){ scanf("%d",&t); while(t--){ ans=0xffffffff; scanf("%d",&n); for(int i=0;i<n;i++) scanf("%lf%lf",&tr[i].x,&tr[i].y); for(int i=0; i<n; i++) for(int j=i+1; j<n; j++) for(int k=j+1; k<n; k++){ a=tr[i].x-tr[k].x; b=tr[j].y-tr[k].y; c=tr[i].y-tr[k].y; d=tr[j].x-tr[k].x; s=fabs(a*b-c*d)/2.0; if(s<=0) continue; ans=min(s,ans); } if(ans==0xffffffff) printf("Impossible\n"); else printf("%.2lf\n",ans); } }


浙公网安备 33010602011771号