Uva 10112 - 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 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
#include<stdio.h> #include<string.h> #define MAXN 600 double cal_area(int i, int j, int k, double (*crd)[2]) { double temp; temp = 0.5*((crd[k][1]-crd[i][1])*(crd[j][0]-crd[i][0])-(crd[j][1]-crd[i][1])*(crd[k][0]-crd[i][0])); if(temp < 0) return -temp; return temp; } int main() { int n, i, j, k, loc, order[MAXN][3], cnt, t, flag; double max, crd[20][2], array[MAXN], temp; while(scanf("%d", &n) != EOF && n) { memset(array, 0, sizeof(array)); memset(order, 0, sizeof(order)); getchar(); for(i=0; i<n; ++i) { getchar(); scanf("%lf%lf", &crd[i][0], &crd[i][1]); getchar(); } cnt = 0; for(i=0; i<n-2; ++i) for(j=i+1; j<n-1; ++j) for(k=j+1; k<n; ++k) { order[cnt][0] = i, order[cnt][1] = j, order[cnt][2] = k; array[cnt] = cal_area(i, j, k, crd); cnt++; } flag = 1; while(flag) { for(t=i=0,max=0; i<cnt; ++i) if(max < array[i]) { t = i; max = array[i]; } for(i=0; i<n; ++i) { if((i != order[t][0]) && (i != order[t][1]) && (i != order[t][2])) { temp = cal_area(i, order[t][0], order[t][1], crd); temp += cal_area(i, order[t][0], order[t][2], crd); temp += cal_area(i, order[t][1], order[t][2], crd); if(max == temp) { array[t] = 0; break; } } } if(i >= n) flag = 0; } printf("%c%c%c\n", order[t][0]+'A', order[t][1]+'A', order[t][2]+'A'); } return 0; }
解题思路:
看OJ上通过率明明就是过半,自己拿到题目的时候还是没有什么思路,(其实是有的,只是否定了这样做而已)题目意思很简单:给你几个点的坐标,然后找出三个点围成的面积最大的三角形,除了满足这个条件外,三角形里面不允许出现其他的点。一直都找不到判断点在三角形内的条件,无奈之下看到数据控制在15的范围内,决心还是一个一个面积算出来,然后判断是否有三个三角形的面积相加等于目前面积最大的那个三角形,如果没有,那就满足这个条件
思路还是不太清晰,草草的写了代码,提交上去排在了1000名外,还真是草草的感觉!
更多内容请关注个人微信公众号 物役记 (微信号:materialchains)