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

『题解』

题目大意是给一些点,这些点可以构成很多三角形。要在所有内部不含其它点的三角形中找出面积最大的。

枚举三角形的三个顶点a,b,c。主要问题在于如何判断三角形内没有其它的点。事实上,若点p在三角形abc内,则叉积(a - b) ^ (p - b),(b - c) ^ (p - c),(c - a) ^ (p - a)是同号的。此方法同样适用与判断点是否在凸多边形内。

三角形abc面积的求法题目给出了,用叉积|(a - b) ^ (c - b) / 2|。在满足条件的三角形中找面积最大的即可。

枚举三角形的三个顶点,对于每个三角形还要枚举判断内部是否包含其它点,因而时间复杂度为O(n ^ 4)。

『C++』

 1 #include <iostream>
 2 #include <cmath>
 3 #include <algorithm>
 4 #include <stdlib.h>
 5 using namespace std;
 6
 7 struct Vector {
 8     double x, y;
 9     Vector() :x(0), y(0) {}
10     Vector(double xx, double yy) :
11         x(xx), y(yy) {}
12 }pts[50];
13
14 int n;
15
16 double operator ^(const Vector &v1, const Vector &v2) {
17     return v1.x * v2.y - v1.y * v2.x;
18 }
19
20 Vector operator -(const Vector &v1, const Vector &v2) {
21     return Vector(v1.x - v2.x, v1.y - v2.y);
22 }
23
24 double Area(const Vector &A, const Vector &B, const Vector &C) {
25     return fabs(0.5 * ((B - A) ^ (C - A)));
26 }
27
28 bool InTriangle(const Vector &A, const Vector &B,
29     const Vector &C, const Vector &P) {
30     double d1 = (A - B) ^ (P - B);
31     double d2 = (B - C) ^ (P - C);
32     double d3 = (C - A) ^ (P - A);
33
34     if ((d1 >= 0 && d2 >= 0 && d3 >= 0) ||
35         (d1 <= 0 && d2 <= 0 && d3 <= 0))
36         return true;
37     return false;
38 }
39
40 bool Check(int pn1, int pn2, int pn3) {
41     for (int i = 0; i < n; i++) {
42         if (i == pn1 || i == pn2 || i == pn3)
43             continue;
44         if (InTriangle(pts[pn1], pts[pn2], pts[pn3], pts[i]))
45             return false;
46     }
47     return true;
48 }
49
50 int main()
51 {
52     while (scanf_s("%d", &n) == 1 && n) {
53         if (n == 0)break;
54
55         double ans = 0;
56         int num[3] = {};
57
58         for (int i = 0; i < n; i++) {
59             char c;
60             cin >> c >> pts[i].x >> pts[i].y;
61         }
62
63         for(int i = 0; i < n; i++)
64             for(int j = i + 1; j < n; j++)
65                 for(int k = j+1; k < n; k++)
66                     if (Check(i, j, k)) {
67                         double area = Area(pts[i], pts[j], pts[k]);
68                         if (ans < area) {
69                             ans = area;
70                             num[0] = i;
71                             num[1] = j;
72                             num[2] = k;
73                         }
74                     }
75
76         printf("%c%c%c\n", ‘A‘ + num[0], ‘A‘ + num[1], ‘A‘ + num[2]);
77     }
78
79     //system("pause");
80     return 0;
81 }

原文地址:https://www.cnblogs.com/Jeffrey-Y/p/10293449.html

时间: 2024-11-09 02:02:12

POJ1569 Myacm Triangles的相关文章

Myacm Triangles

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 fie

ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)

Description Problem B: Myacm Triangles 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

18.12.17 POJ 1569 Myacm Triangles

描述 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

UVA题目分类

题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics 10300 - Ecological Premium 458 - The Decoder 494 - Kindergarten Counting Game 414 - Machined Surfaces 490 - Rotating Sentences 445 - Marvelous Mazes

HOJ 题目分类

转自:http://blog.sina.com.cn/s/blog_65f3869301011a1o.html ******************************************************************************* 简单题(包括枚举,二分查找,(复杂)模拟,基础数据结构(栈.队列),杂题等 ****************************************************************************

1.5.1 Number Triangles 数字金字塔

★1.5.1 Number Triangles 数字金字塔 一.题目描述 考虑在下面被显示的数字金字塔. 写一个程序来计算从最高点开始在底部任意处结束的路径经过数字的和的最大. 每一步可以走到左下方的点也可以到达右下方的点. 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 在上面的样例中,从7 到 3 到 8 到 7 到 5 的路径产生了最大和:30 PROGRAM NAME: numtri 18 INPUT FORMAT 第一个行包含 R(1<= R<=1000) ,表示行的数目

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

UVALive 7077 Little Zu Chongzhi&#39;s Triangles (有序序列和三角形的关系)

这个题……我上来就给读错了,我以为最后是一个三角形,一条边可以由多个小棒组成,所以想到了状态压缩各种各样的东西,最后成功了……结果发现样例过不了,三条黑线就在我的脑袋上挂着,改正了以后我发现N非常小,想到了回溯每个棍的分组,最多分5组,结果发现超时了……最大是5^12 =  244,140,625,厉害呢…… 后来想贪心,首先想暴力出所有可能的组合,结果发现替换问题是一个难题……最后T T ,我就断片了.. 等看了别人的办法以后,我才发现我忽视了三角形的特性,和把数据排序以后的特点. 如果数据从

Codeforces Gym 100015F Fighting for Triangles 状态压缩DP

F Fighting for Triangles Description Andy and Ralph are playing a two-player game on a triangular board that looks like the following: 1 234 5 7 86 910 11 13 14 16 1712 15 18 At each turn, a player must choose two adjacent vertices and draw a line se