题目大意:给出一系列点的坐标,找出三个点,使得这三个点连起来组成的三角形,不包含其他点。且这是能找到的面积最大的三角形。
解题思路:最多15个点,直接三个循环暴力,主要是判断其他点是否在三角形内或三角形上,判断方法有挺多,什么有向法巴拉巴拉。我写了最好懂的,就是面积法。判断SΔABC是否等于SΔABP+SΔACP+SΔBCP。如果等于说明P在三角形内或三角形上。
ac代码:
#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
struct node{
char name;
double x;
double y;
}point[20];
int n, temp;
char one, two, three;
double sum;
bool contain(int focus, int a, int b, int c)
{
double x1, x2, y1, y2;
double s1, s2, s3, sum;
x1 = point[c].x - point[a].x;
x2 = point[b].x - point[a].x;
y1 = point[c].y - point[a].y;
y2 = point[b].y - point[a].y;
sum = fabs(x1*y2 - x2*y1);
s1 = fabs(x2*(point[focus].y-point[a].y)
- y2*(point[focus].x-point[a].x));
s2 = fabs(x1*(point[focus].y-point[a].y)
- y1*(point[focus].x-point[a].x));
x1 = point[focus].x - point[c].x;
x2 = point[focus].x - point[b].x;
y1 = point[focus].y - point[c].y;
y2 = point[focus].y - point[b].y;
s3 = fabs(x1*y2 - x2*y1);
if (sum == s1+s2+s3)
return 1;
return 0;
}
int judge(int a, int b, int c)
{
int t1, t2, t3, t4;
for (int i=0; i<n; i++)
if (i != a && i != b && i != c)
if (contain(i, a, b, c))
return 0;
t1 = point[c].y - point[a].y;
t2 = point[b].x - point[a].x;
t3 = point[b].y - point[a].y;
t4 = point[c].x - point[a].x;
return fabs(0.5*(t1*t2-t3*t4));
}
bool compare(node a, node b)
{
return a.name < b.name;
}
int main()
{
while (scanf("%d", &n)!=EOF && n){
for (int i=0; i<n; i++){
getchar();
scanf("%c%lf%lf", &point[i].name, &point[i].x, &point[i].y);
}
sum = 0;
sort(point, point+n, compare);
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++){
temp = judge(i, j, k);
if (temp > sum){
one = point[i].name;
two = point[j].name;
three = point[k].name;
sum = temp;
}
}
printf("%c%c%c\n", one, two, three);
}
return 0;
}