[ACM]hdu Herding(枚举+三角形面积)
2014-02-24 14:19
369 查看
Herding
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)Total Submission(s) : 5 Accepted Submission(s) : 2
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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 theircartesian 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解题思路:
本题就是给定几个点,然后从中选择几个点围成一定的面积,要求这个最小的面积,一开始想复杂了,以为四个或五个的点也能组成最小面积,其实最小的是三个点,因为四个或五个点其中里面选三个点肯定面积要小,所以本题的本质也就是求最小三角形的面积。
代码:
#include <iostream> #include <cmath> #include <iomanip> using namespace std; struct point { double x,y; }p[103];//构造点 double area(point p1,point p2,point p3) { return abs(p1.x*p2.y+p1.y*p3.x+p2.x*p3.y-p2.y*p3.x-p1.x*p3.y-p1.y*p2.x)/2.0; }//计算三角形的面积 int main() { int t,n; cin>>t; for(int ni=1;ni<=t;ni++) { cin>>n; for(int i=1;i<=n;i++) cin>>p[i].x>>p[i].y; int flag=0; double Min=9999999;//用于最后输出 for(int i=1;i<=n;i++)//枚举三个不同的点,三重循环完成 { for(int j=i+1;j<=n;j++) { for(int k=j+1;k<=n;k++) { double temp=area(p[i],p[j],p[k]); if(temp<Min&&temp!=0)//temp不为0才能构成三角形 { Min=temp; flag=1; } } } } if(flag) cout<<setiosflags(ios::fixed)<<setprecision(2)<<Min<<endl; else cout<<"Impossible"<<endl; } return 0; }
要点:给定三个点,求其面积
double area(point p1,point p2,point p3) { return abs(p1.x*p2.y+p1.y*p3.x+p2.x*p3.y-p2.y*p3.x-p1.x*p3.y-p1.y*p2.x)/2.0; }
即求行列式
1 1 1
x1 x2 x3
y1 y2 y3
相关文章推荐
- [ACM]hdu Herding(枚举+三角形面积)
- poj 1569 Myacm Triangles 判断点在三角形内+求三角形面积
- hdu 4709:Herding(叉积求三角形面积+枚举)
- ACM-正三角形的外接圆面积(经典算法)
- [ACM] HDU 5135 Little Zu Chongzhi's Triangles (一堆木棍组成三角形最大面积)
- 南阳理工ACM 题目67 三角形面积
- ACM-三角形面积
- 【枚举】求三角形最大面积问题
- HDU4709 [枚举三点求最小三角形面积]
- hdu4709 herding 计算几何 三角形面积 枚举
- 4-1设计求三角形周长和面积的类
- nyoj 三角形面积
- 三角形面积
- C求圆、梯形、三角形的面积和一元二次方程的根(实虚)
- 陈老师的多校联合20140816||SPOJ AMR11B 三角形的有向面积
- 区间DP 最大面积最小的三角形划分
- NYOJ67三角形面积 VS NYOJ68三点顺序
- InCircle 求三角形面积
- 【java学习记录】7.定义一个接口ArearInterface,其中包含一个方法,计算面积三角形、矩形、圆形的面积
- 木棒三角形-枚举