uva 10112 Myacm Triangles(收索+公式判断)

942 篇文章 2 订阅
99 篇文章 0 订阅

Myacm Triangles

Source file:triangle.{ccppjavapas}
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 (x1y1), (x2y2), and (x3y3) is the absolute value of

0.5 × [( y 3  -  y 1)( x 2  -  x 1- ( y 2  -  y 1)( x 3  -  x 1)].

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<iostream>
#include<string.h>
#include<math.h>
using namespace std;

#define N 105
struct coor{
	char name;
	int x;
	int y;
};

coor s[N];
int bo[N];
int n;

double area(int a, int b, int c){
	double t = 0.5 * ( (s[c].y - s[a].y) * (s[b].x - s[a].x) - (s[b].y - s[a].y) * (s[c].x - s[a].x));
	if (t > 0)
		return t;
	else 
		return -t;
}

int judge(int a, int b, int c, double sum){
	double t = 0;
	for (int i = 0; i < n; i++){
		if (bo[i])	continue;
		t = area(a, b, i) + area(a, c, i) + area(b, c, i);
		if (t == sum)
			return 0;
	}
	return 1;
}

int main(){

	while (cin >> n, n){

		// Init.
		memset(s, 0, sizeof(s));
		memset(bo, 0, sizeof(bo));

		for (int i = 0; i < n; i++)
			cin >> s[i].name >> s[i].x >> s[i].y;

		// Count.
		int a = 0, b = 0, c = 0;
		double sum = 0;
		for (int i = 0; i < n; i++){

			if(bo[i]) continue;

			bo[i] = 1;

			for (int j = 0; j < n; j++){

				if (bo[j]) continue;

				bo[j] = 1;

				for (int k = 0; k < n; k++){

					if (bo[k]) continue;

					bo[k] = 1;

					double t = area(i, j, k);

					if (sum < t && judge(i, j, k, t)){
						sum = t;
						a = i;
						b = j;
						c = k;
					}
					bo[k] = 0;
				}
				bo[j] = 0;
			}
			bo[i] = 0;
		}

		// Print.
		cout << s[a].name << s[b].name << s[c].name << endl;
	}
	return 0;}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值