UVA 10112 Myacm Triangles

4205 단어
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 × [(
y


y
1)(
x


x
1) 
- (
y


y
1)(
x


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

제목: 점 을 입력 하 세 요.그 중 세 개의 점 이 삼각형 을 구성 하고 가장 크 며 다른 삼각형 은 모두 내부 에 없다.
해법: 제목 은 공식 이 있다.면적 을 구하 면 구하 기 쉽다.관건 은 어떻게 판단 하 느 냐 하 는 것 이다.
나 는 이렇게 한다. 예 를 들 어 삼각형 abc 와 p.만약 면적 Sabc = = Sabp + Sacp + Sbcp 는 p 가 삼각형 내부 에 있다 는 것 을 설명 합 니 다.
이렇게 되면 폭력 을 판정 하기 쉽다.
 
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
using namespace std;
int n;
char x[3];
struct Q
{
    char zi;
    int x;
    int y;
} q[25];

int judge(int s1, int s2, int s3, int ss)
{
    for (int i = 0; i < n; i ++)
    {
	if (i != s1 && i != s2 && i != s3)
	{
	    int s11 = abs((q[i].y - q[s1].y) * (q[s2].x - q[s1].x) - (q[s2].y - q[s1].y) * (q[i].x - q[s1].x));
	    int s22 = abs((q[i].y - q[s2].y) * (q[s3].x - q[s2].x) - (q[s3].y - q[s2].y) * (q[i].x - q[s2].x));
	    int s33 = abs((q[i].y - q[s1].y) * (q[s3].x - q[s1].x) - (q[s3].y - q[s1].y) * (q[i].x - q[s1].x));
	    if (ss == s11 + s22 + s33)
	    {
		return 0;
	    }
	}
    }
    return 1;
}
int main()
{
    int tt = 1;
    while (scanf("%d", &n) != EOF && n)
    {
	getchar();
	int max = -99999999;
	for (int i = 0; i < n; i ++)
	{
	    scanf("%c%d%d%*c", &q[i].zi, &q[i].x, &q[i].y);    
	}
	for (int i = 0; i < n; i ++)
	    for (int j = i + 1; j < n; j ++)
		for (int k = j + 1; k < n; k ++)
		{
		    int s = abs((q[k].y - q[i].y) * (q[j].x - q[i].x) - (q[j].y - q[i].y) * (q[k].x - q[i].x));
		    if (max < s && judge(i, j, k, s)) 
		    {
			max = s;
			x[0] = q[i].zi;
			x[1] = q[j].zi;
			x[2] = q[k].zi;
			x[3] = '\0';
		    }
		}
	sort(x, x + 3);
	printf("%c%c%c
", x[0], x[1], x[2]); } return 0; }

좋은 웹페이지 즐겨찾기