POJ1569 Myacm Triangles 判斷點與三角形的關係

Description


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)]. 

Input

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. 

Output

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.

Sample 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

Sample Output

BEF
BCD

Source


求一些點所構成的三角形中,內部(含邊)不含其他點的面積最大的三角形。

由於點很少,枚舉所有三角形,在判斷內部是否有點即可。

判斷內部是否有點有幾種辦法:

一是判斷一點與該三角形任意兩點組成的三角形面積之和是否等於原三角形;

二是判斷點是否在邊上以及是否與該三角形任意一點相對於其餘兩點在同側。

兩種方法難易很明顯。

求面積可用題中所給公式。

#include <iostream>
#include <cstdio>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
using namespace std;
typedef long long LL;
double EPS=1e-6;
struct maxans{
    double maxn;
    char a,b,c;
}ans;
struct CNode{
    double x,y;
    CNode(double xx,double yy): x(xx),y(yy){}
    CNode(){}
    double operator ^ (const CNode & v) const{
        return x*v.y-v.x*y;
    }
}node[20];

CNode operator - (CNode p,CNode q)
{
    return CNode(p.x+q.x,p.y+q.y);
}
double Area(int i,int j,int k)
{
    return fabs((node[k].y-node[i].y)*(node[j].x-node[i].x)-(node[j].y-node[i].y)*(node[k].x-node[i].x));
}

bool IsZero(double x)
{
    return -EPS<x&&x<EPS;
}
int SideOfLine(int p,int a,int b)
{
    double result = (node[b] - node[a]) ^ (node[p] - node[a]);
    if (IsZero(result)) return 0;
    else if (result > 0) return 1;
    else return -1;
}
int main()
{
    int n;
    double x,y;
    char ch;
    char a[20];
    while(~scanf("%d",&n),n){
        for(int i=0;i<n;i++){
            getchar();
            scanf("%c%lf%lf",&ch,&x,&y);
            node[i]=CNode(x,y);
            a[i]=ch;
        }
        ans.maxn=0;
        for(int i=0;i<n-2;i++){
            for(int j=i+1;j<n-1;j++){
                for(int k=j+1;k<n;k++){
                    int flag=1;
                    int temp=Area(i,j,k);
                    for(int t=0;t<n;t++){
                        if(t!=i&&t!=j&&t!=k){
                            if(temp==(Area(t,i,j)+Area(t,j,k)+Area(t,k,i))){
                                flag=0;
                                break;
                            }
                        }
                    }
                    if(flag){
                        if(temp>ans.maxn){
                            ans.maxn=temp;
                            ans.a=a[i];
                            ans.b=a[j];
                            ans.c=a[k];
                        }
                    }
                }
            }
        }
        printf("%c%c%c\n",ans.a,ans.b,ans.c);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章