hdu 4709:Herding(叉积求三角形面积+枚举)
Herding
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1125 Accepted Submission(s): 325
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.
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
Recommend
叉积求三角形面积+枚举。
刚接触计算几何,这道题被坑了好几遍,好吧,我承认是我心急了,还是要沉下心来把基础知识先看一遍。
回到这道题,三层循环枚举出构成三角形的三个点,然后用叉积求出三角形面积,如果结果等于0,说明当前三个点构不成三角形,枚举所有情况,输出符合条件的最小面积。
另外不知道为什么海伦公式一直错。
1 #include <iostream>
2 #include <iomanip>
3 using namespace std;
4 struct point{
5 double x,y;
6 }P[105];
7 double getS(point a,point b,point c) //叉积求面积
8 {
9 return ((b.x - a.x) * (c.y - a.y) - (b.y - a.y)*(c.x - a.x))/2;
10 }
11 int main()
12 {
13 int T,N;
14 cin>>T;
15 cout<<setiosflags(ios::fixed)<<setprecision(2);
16 while(T--){
17 cin>>N;
18 bool flag = false;
19 double minS=999999999;
20 for(int i=1;i<=N;i++)
21 cin>>P[i].x>>P[i].y;
22 for(int i=1;i<=N;i++)
23 for(int j=1;j<=N;j++)
24 if(i!=j)
25 for(int k=1;k<=N;k++)
26 if(k!=i && k!=j){
27 double t = getS(P[i],P[j],P[k]);
28 if(t<0) t=-t;
29 if(t<minS && t!=0){
30 minS=t;
31 flag = true;
32 }
33 }
34 if(flag)
35 cout<<minS<<endl;
36 else
37 cout<<"Impossible"<<endl;
38 }
39 return 0;
40 }
Freecode : www.cnblogs.com/yym2013