Description

 

Problem B: Myacm Triangles

Problem B: Myacm Triangles

Source file: triangle.{c, cpp, java, pas}
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 (x1, y1), (x2, y2), and (x3, y3) is the absolute value of

0.5 × [(    y3-y1)(    x2-x1)     - (    y2-y1)(    x3-x1)].

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

这个题目关键在于如何判断某个点是否在三角形中,如果解决了直接暴力枚举即可。

要判断某个点是否在三角形中,此处采用了向量积:

只要这个点与三角形三个点的连线是顺时针或逆时针依次排列,那么这个点便在三角形中,于是只要判断,相邻两个连线的数量积是否恒为正数或负数。

代码:

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstdlib>
4 #include <cstring>
5 #include <cmath>
6 #include <algorithm>
7 #include <set>
8 #include <map>
9 #include <vector>
10 #include <queue>
11 #include <string>
12 #define inf 0x3fffffff
13 #define eps 1e-10
14
15 using namespace std;
16
17 struct point
18 {
19 int x, y;
20
21 double xj(point a, point b)
22 {
23 double ans;
24 ans = (b.y-y) * (a.x-x) - (a.y-y) * (b.x-x);
25 return ans;
26 }
27
28 double area(point a, point b)
29 {
30 return fabs(xj(a, b)) / 2;
31 }
32
33 bool in(point a, point b, point c)
34 {
35 double i, j, k;
36 i = xj(a, b);
37 j = xj(b, c);
38 k = xj(c, a);
39 if (i <= 0 &&
40 j <= 0 &&
41 k <= 0)
42 return 1;
43 if (i >= 0 &&
44 j >= 0 &&
45 k >= 0)
46 return 1;
47 return 0;
48 }
49 };
50
51 point v[16];
52 int n;
53 int ans[3];
54
55 void Input()
56 {
57 char ch[3];
58 for (int i = 0; i < n; ++i)
59 scanf("%s%d%d", ch, &v[i].x, &v[i].y);
60 }
61
62 void Output()
63 {
64 sort(ans, ans+3);
65 for (int i = 0; i < 3; ++i)
66 printf("%c", ans[i]+'A');
67 printf("\n");
68 }
69
70 void qt()
71 {
72 double Max, t;
73 bool flag = 1, ok;
74 for (int i = 0; i < n; ++i)
75 for (int j = i+1; j < n; ++j)
76 for (int k = j+1; k < n; ++k)
77 {
78 ok = 1;
79 for (int x = 0; x < n; ++x)
80 {
81 if (x == i ||
82 x == j ||
83 x == k)
84 continue;
85 if (v[x].in(v[i], v[j], v[k]))
86 {
87 ok = 0;
88 break;
89 }
90 }
91 if (!ok)
92 continue;
93 t = v[i].area(v[j], v[k]);
94 if (flag)
95 {
96 Max = t;
97 ans[0] = i;
98 ans[1] = j;
99 ans[2] = k;
100 flag = 0;
101 }
102 else if (Max < t)
103 {
104 Max = t;
105 ans[0] = i;
106 ans[1] = j;
107 ans[2] = k;
108 }
109 }
110 }
111
112 int main()
113 {
114 //freopen("test.txt", "r", stdin);
115 while (scanf("%d", &n) != EOF && n)
116 {
117 Input();
118 qt();
119 Output();
120 }
121 return 0;
122 }

ACM学习历程——UVA10112 Myacm Triangles(计算几何,多边形与点的包含关系)的更多相关文章

  1. ACM学习历程—BestCoder 2015百度之星资格赛1004 放盘子(策略 && 计算几何)

    Problem Description 小度熊喜欢恶作剧.今天他向来访者们提出一个恶俗的游戏.他和来访者们轮流往一个正多边形内放盘子.最后放盘子的是获胜者,会赢得失败者的一个吻.玩了两次以后,小度熊发 ...

  2. ACM学习历程—FZU2148 Moon Game(计算几何)

    Moon Game Description Fat brother and Maze are playing a kind of special (hentai) game in the clearl ...

  3. ACM学习历程—FZU 2144 Shooting Game(计算几何 && 贪心 && 排序)

    Description Fat brother and Maze are playing a kind of special (hentai) game in the playground. (May ...

  4. ACM学习历程—FZU 2140 Forever 0.5(计算几何 && 构造)

    Description   Given an integer N, your task is to judge whether there exist N points in the plane su ...

  5. ACM学习历程—HDU4720 Naive and Silly Muggles(计算几何)

    Description Three wizards are doing a experiment. To avoid from bothering, a special magic is set ar ...

  6. ACM学习历程—HDU1392 Surround the Trees(计算几何)

    Description There are a lot of trees in an area. A peasant wants to buy a rope to surround all these ...

  7. 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始

    以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告

  8. ACM学习历程—HDU 5512 Pagodas(数学)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5512 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是给了初始的集合{a, b},然后取集合里 ...

  9. ACM学习历程—HDU5521 Meeting(图论)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5521 学习菊苣的博客,只粘链接,不粘题目描述了. 题目大意就是一个人从1开始走,一个人从n开始走.让最 ...

随机推荐

  1. PHP计划任务:如何使用Linux的Crontab执行PHP脚本(转)

    我们的PHP程序有时候需要定时执行,我们可以使用ignore_user_abort函数或是在页面放置js让用户帮我们实现.但这两种方法都不太可靠,不稳定.我们可以借助Linux的Crontab工具来稳 ...

  2. 【Linux】线程并发拷贝程序

    据说大连某211高校的李教授越来越重口.不仅延续要求他所带的每个本科班.都要写一份线程并发拷贝程序的传统,并且还開始规定不能用Java语言写作.导致我之前写的<[Java]线程并发拷贝程序> ...

  3. C#中判断某个值是否存在于枚举

    我有一个枚举类型: #region -酒的种类- public enum WineType { 白酒 = 3, 葡萄酒 = 4, 洋酒 = 5, 老年陈酒 = 16, 啤酒 = 17 } #endre ...

  4. 写一段代码,判断一个包括'{','[','(',')',']','}'的表达式是否合法(注意看样例的合法规则。) 给定一个表达式A,请返回一个bool值,代表它是否合法。

    这道题比较奇怪,它的匹配规则并不是我们平时想想的那种匹配规则,例如:平时的匹配规则是().{}.[]才能匹配,本题中(和} .].)都能匹配.所以做题时要好好审题.另外,本题中给的测试用例是错误的. ...

  5. nginx支持pathinfo模式

    很久不使用apache了,渐渐对apache感到陌生,因为朋友有个ZendFramework框架从apache移到nginx下,需要pathinfo模式支持.网上海搜于是开始搜索nginx+pathi ...

  6. (翻译) Container Components

    react.js javascript   这篇文章翻译自Medium的一篇文章:Container Components 选择这篇文章翻译的原因是,在刚接触React的时候,这篇文章很好的指引我了解 ...

  7. Tinker 热修复框架 简单上手教程

    当你们看到Tinker的时候是不是有点愣逼这个是什么东西? 简单来说就是不需要重新下载app和重新安装app 来进行更新app的技术框架. 看看这个吧,我也是才学习 ,先做个学习记录 参考:Tinke ...

  8. iOS项目 -- 模仿花椒直播做的第一层次界面

    公最近直播比较火爆,我也跟跟风,自己做一个直播app, 现在打算用金山云直播的,但是去注册的时候,联系那边的工作人员,他们讲使用金山云直播要有公司和他们线下签约才能授权开放直播平台. 怎么办呢?于是我 ...

  9. 提高Interface Builder高效工作的8个技巧

    本文转载至 http://www.cocoachina.com/ios/20141106/10151.html iOS开发Interface Builder 本文译自:8 Tips for worki ...

  10. 利用.dSYM跟.app文件准确定位Crash位置

     本文转载至  http://blog.csdn.net/lvxiangan/article/details/28102629       利用.dSYM和.app文件准确定位Crash位置首先,确保 ...