UVA 10112 - Myacm Triangles

本文介绍了一种通过遍历所有可能的三点组合来找到给定点集中最大面积且不含其他点的三角形的方法。使用面积比较法判断点是否位于特定三角形内部或边上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:给出一系列点的坐标,找出三个点,使得这三个点连起来组成的三角形,不包含其他点。且这是能找到的面积最大的三角形。

解题思路:最多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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值