题目就是给出一些点,求哪一个三角形不包含其他点且面积最大。题目中也给出了一个计算面积的公式。
要注意的是判断double类型面积相等注意精度问题。写的时候忘记了给面积加绝对值
题目:
Problem B: Myacm Triangles
Source file: | triangle.{c, cpp, java, pas} |
Input file: | triangle.in |
Output file: | triangle.out |
There has been considerable archeological work on the ancient Myacm culture. Many artifacts have been found in what have been called power fields: a fairly small area, less than 100 meters square where there are from four to fifteen tall monuments with crystals on top. Such an area is mapped out above. Most of the artifacts discovered have come from inside a triangular area between just three of the monuments, now called the power triangle. After considerable analysis archeologists agree how this triangle is selected from all the triangles with three monuments as vertices: it is the triangle with the largest possible area that does not contain any other monuments inside the triangle or on an edge of the triangle. Each field contains only one such triangle.
Archeological teams are continuing to find more power fields. They would like to automate the task of locating the power triangles in power fields. Write a program that takes the positions of the monuments in any number of power fields as input and determines the power triangle for each power field.
A useful formula: the area of a triangle with vertices (x1, y1), (x2, y2), and (x3, y3) is the absolute value of
0.5 × [(y3 - y1)(x2 - x1) - (y2 - y1)(x3 - x1)].
For each power field there are several lines of data. The first line is the number of monuments: at least 4, and at most 15. For each monument there is a data line that starts with a one character label for the monument and is followed by the coordinates of the monument, which are nonnegative integers less than 100. The first label is A, and the next is B, and so on.
There is at least one such power field described. The end of input is indicated by a 0 for the number of monuments. The first sample data below corresponds to the diagram in the problem.
For each power field there is one line of output. It contains the three labels of the vertices of the power triangle, listed in increasing alphabetical order, with no spaces.
Example input:
6 A 1 0 B 4 0 C 0 3 D 1 3 E 4 4 F 0 6 4 A 0 0 B 1 0 C 99 0 D 99 99 0
Example output:
BEF BCD
代码:1 #include <iostream> 2 #include <math.h> 3 using namespace std; 4 const int maxn =30; 5 char P[maxn]; 6 int x[maxn]; 7 int y[maxn]; 8 int N; 9 10 double area(double ax,double ay,double bx,double by,double cx,double cy) 11 { 12 //0.5 ¡Á [(y3 - y1)(x2 - x1) - (y2 - y1)(x3 - x1)]. 13 double ans ; 14 ans = 0.5 * ((cy-ay)*(bx-ax)-(by-ay)*(cx-ax)); 15 ans = ans<0?-ans:ans; 16 return ans; 17 } 18 bool check(int o,int a,int b,int c,double abc) 19 { 20 double x1 = area(x[o],y[o],x[a],y[a],x[b],y[b]); 21 double x2 = area(x[o],y[o],x[a],y[a],x[c],y[c]); 22 double x3 = area(x[o],y[o],x[b],y[b],x[c],y[c]); 23 if( fabs(abc-x1-x2-x3)<1e-9 )return true; //ÔÚÔ²ÄÚ 24 else return false; 25 } 26 27 int ansa,ansb,ansc,ansmax; 28 void solve() 29 { 30 ansmax=0; 31 for(int i=0;i<N;i++) 32 { 33 for(int j=i+1;j<N;j++) 34 { 35 for(int k=j+1;k<N;k++) 36 { 37 int flag=1; 38 double s = area(x[i],y[i],x[j],y[j],x[k],y[k]); 39 40 for(int m =0;m<N;m++) 41 { 42 if(m==i||m==j||m==k)continue; 43 // cout<<i<<" "<<j<<" "<<k<<endl; 44 if(check(m,i,j,k,s)) 45 { 46 flag=0; 47 break; 48 } 49 } 50 if(flag) 51 { 52 if(s>ansmax) 53 { 54 ansa = i; ansb=j;ansc=k; 55 ansmax =s; 56 // cout<<ansa<<" "<<ansb<<" "<<ansc<<endl; 57 } 58 } 59 else 60 flag=1; 61 } 62 } 63 } 64 } 65 66 67 68 int main() 69 { 70 71 while(cin>>N && N) 72 { 73 ansa=ansb=ansc=ansmax=0; 74 for(int i=0;i<N;i++) 75 { 76 cin>>P[i]; 77 cin>>x[i]>>y[i]; 78 } 79 solve(); 80 //cout<<ansa<<ansb<<ansc<<endl; 81 cout<<P[ansa]<<P[ansb]<<P[ansc]<<endl; 82 } 83 84 85 86 return 0; 87 }