UVA 10112 Myacm Triangles

来源:互联网 发布:域名注册批量查询 编辑:程序博客网 时间:2024/05/16 06:24

题目

Myacm三角形

分析

给出一些点的坐标,要求出最大面积且不包含任意点(点在边上也视为在三角形内)的三角形的三个点。
另给出三角形面积公式

S=12×[(y3y1)(x2x1)(y2y1)(x3x1)]

思路

遍历所有的点做三角形,再对每一个三角形遍历其他所有点,判断该点是否在该三角形内,可以采用连点三角形的方式,如果连结产生的三角形总面积大于原三角形面积,那么点在形外。
形成三角形再比较面积,此时因为比较的量为面积,其都含有12,所以可以忽略。

代码

#include <stdio.h>#include <math.h>int point[16][2];char label[16];int res[3];int main(void){    int n, i, j, k, t, s, a, b, c, max, flag;    while (scanf("%d\n", &n) && n > 0) {        for (i = 0; i < n; i++)            scanf("%c %d %d\n", &label[i], &point[i][0], &point[i][1]);        max = 0;        for (i = 0; i < n-2; i++)            for (j = i+1; j < n-1; j++)                for (k = j+1; k < n; k++) {                    s = fabs((point[k][1] - point[i][1]) * (point[j][0] - point[i][0]) -                             (point[j][1] - point[i][1]) * (point[k][0] - point[i][0]));                    flag = 0;                    for (t = 0; t < n; t++) {                        if (t == i || t == j || t == k) continue;                        a = fabs((point[t][1] - point[i][1]) * (point[j][0] - point[i][0]) -                                 (point[j][1] - point[i][1]) * (point[t][0] - point[i][0]));                        b = fabs((point[k][1] - point[i][1]) * (point[t][0] - point[i][0]) -                                 (point[t][1] - point[i][1]) * (point[k][0] - point[i][0]));                        c = fabs((point[k][1] - point[t][1]) * (point[j][0] - point[t][0]) -                                 (point[j][1] - point[t][1]) * (point[k][0] - point[t][0]));                        if (a + b + c == s) {                            flag = 1;                            break;                        }                    }                    if (!flag) {                        if (max < s) {                            max = s;                            res[0] = i;                            res[1] = j;                            res[2] = k;                        }                    }                }        printf("%c%c%c\n", label[res[0]], label[res[1]], label[res[2]]);    }    return 0;}
0 0
原创粉丝点击