POJ 1569 && Myacm Triangles (枚举+叉积)

Myacm Triangles
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 1280 Accepted: 718
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,看 (b-a)^(c-b)符号得出是顺时针序还是逆时针 序,然后用叉积判断其他点是否在三角形内 (边为逆时针序的情况下,点同时在三条边的 左边则为在三角形内。此法同样适用于判断点 是否在凸多边形内)

判断一个点(d)是否再三角形里面(以a,b,c为端点),可以先计算出
a=(ta-tb)^(td-tb);
b=(tb-tc)^(td-tc);
c=(tc-ta)^(td-ta);
如果a,b,c三者同号,那么就在里面

首先我们写出向量的几种必要函数,构造出结构体贮存,再加上枚举判断就可以了

AC代码如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=55;
int n,fa,fb,fc;
char c;
struct vec{
	double x,y;
	vec() : x(0),y(0) {}
	vec(double xx,double yy):x(xx),y(yy){}
}f[maxn];

double operator ^(const vec &ta,const vec &tb)
{
	return (ta.x*tb.y-ta.y*tb.x);
}
 
vec operator - (const vec &ta,const vec &tb)
{
	return vec(ta.x-tb.x,ta.y-tb.y);
} 

double area(const vec &ta,const vec &tb,const vec &tc)
{
	return fabs(0.5*((tb-ta)^(tc-ta)));
}

int check(const vec &ta,const vec &tb,const vec &tc,const vec &td)
{
	double a,b,c;
	a=(ta-tb)^(td-tb);
	b=(tb-tc)^(td-tc);
	c=(tc-ta)^(td-ta);
	if((a>=0&&b>=0&&c>=0)||(a<=0&&b<=0&&c<=0))
	return 1;
	else
	return 0;
}

int meiju(int ta,int tb,int tc)
{
	for(int i=0;i<n;i++)
	{
		if(i==ta||i==tc||i==tb)
		{
			continue;
		}
		else
		{
			if(check(f[ta],f[tb],f[tc],f[i])==1)
			return 0;
		}
	}
	return 1;
}

void input()
{
	for(int i=0;i<n;i++)
	{
		cin>>c>>f[i].x>>f[i].y;
	}
}

void ans()
{
	double maxx=-1;
	for(int i=0;i<n;i++)
	{
		for(int j=i+1;j<n;j++)
		{
			for(int k=j+1;k<n;k++)
			{
				if(meiju(i,j,k)==1)
				{
					double temp=area(f[i],f[j],f[k]);
					if(temp>=maxx)
					{
						maxx=temp;
						fa=i;
						fb=j;
						fc=k;
					}
				}
			}
		}
	}
}
void output()
{
	printf("%c%c%c\n",'A'+fa,'A'+fb,'A'+fc);
}
int main()
{
	while(~scanf("%d",&n))
	{
		if(n==0)
		break;
		fa=0,fb=0,fc=0;
		input();
		ans();
		output();
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值