UVA 10112 - Myacm Triangles

来源:互联网 发布:楼天城 知乎 编辑:程序博客网 时间:2024/04/28 19:40
 
UVA <wbr>10112 <wbr>- <wbr>Myacm <wbr>Triangles <wbr>解题报告

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:

6A 1 0B 4 0C 0 3D 1 3E 4 4F 0 64A 0 0B 1 0C 99 0D 99 990

Example output:

BEF BCD
题解:
       题目中让求解面积最大,且三个柱子组成的三角形中没有其他的的柱子。先用面积法求解这样的三角形,在用此方法求解面积。在用sort排序就可以了。代码:
 #include <iostream>#include <cstdio>#include <cmath>#include <algorithm>using namespace std;double mianji(double x1,double y1,double x2,double y2,double x3,double y3){    return 0.5*fabs((y3 - y1)*(x2 - x1) - (y2 - y1)*(x3 - x1));}struct node{    char a,b,c;    double s;}a[500];bool cmp(node a,node b){   return a.s>b.s;}int main(){    int n,i,j,k,p;    while(scanf("%d",&n)!=EOF&&n)    {        getchar();        char c[20];         double b[20][2];         int sum=0;         for(i=0;i<n;i++)         {scanf("%c %lf %lf",&c[i],&b[i][0],&b[i][1]);            getchar();}         for(i=0;i<n-2;i++)         for(j=i+1;j<n-1;j++)         for(k=j+1;k<n;k++)         {             double s0=mianji(b[i][0],b[i][1],b[j][0],b[j][1],b[k][0],b[k][1]);             for(p=0;p<n;p++)             {                 double s1=0,s2=0,s3=0;                 if(p!=i&&p!=j&&p!=k)                 {                     s1=mianji(b[p][0],b[p][1],b[j][0],b[j][1],b[k][0],b[k][1]);                     s2=mianji(b[i][0],b[i][1],b[p][0],b[p][1],b[k][0],b[k][1]);                     s3=mianji(b[i][0],b[i][1],b[j][0],b[j][1],b[p][0],b[p][1]);                 }                 if(s0==(s1+s2+s3))  break;             }             if(p==n)             {                 a[sum].a=c[i];                 a[sum].b=c[j];                 a[sum].c=c[k];                 a[sum++].s=s0;             }         }        sort(a,a+sum,cmp) ;        printf("%c%c%c\n",a[0].a,a[0].b,a[0].c);    }    return 0;} 

 
原创粉丝点击