[UVA][凸包] 209 - Triangular Vertices@Morris' Blog|PChome Online 人新台
2012-12-15 09:31:21| 人789| 回0 | 上一篇 | 下一篇

[UVA][凸包] 209 - Triangular Vertices

0 收藏 0 0 站台


 Triangular Vertices 

Consider the points on an infinite grid of equilateral triangles as shown below:

Note that if we number the points from left to right and top to bottom, then groups of these points form the vertices of certain geometric shapes. For example, the sets of points {1,2,3} and {7,9,18} are the vertices of triangles, the sets {11,13,26,24} and {2,7,9,18} are the vertices of parallelograms, and the sets {4,5,9,13,12,7} and {8,10,17,21,32,34} are the vertices of hexagons.

Write a program which will repeatedly accept a set of points on this triangular grid, analyze it, and determine whether the points are the vertices of one of the following ``acceptable" figures: triangle, parallelogram, or hexagon. In order for a figure to be acceptable, it must meet the following two conditions:

 1) Each side of the figure must coincide with an edge in the grid. 

and 2) All sides of the figure must be of the same length.

Input

The input will consist of an unknown number of point sets. Each point set will appear on a separate line in the file. There are at most six points in a set and the points are limited to the range 1..32767.

Output

For each point set in the input file, your program should deduce from the number of points in the set which geometric figure the set potentially represents; e.g., six points can only represent a hexagon, etc. The output must be a series of lines listing each point set followed by the results of your analysis.

Sample Input

1 2 3 11 13 29 31 26 11 13 24 4 5 9 13 12 7 1 2 3 4 5 47 11 13 23 25

Sample Output

1 2 3 are the vertices of a triangle 11 13 29 31 are not the vertices of an acceptable figure 26 11 13 24 are the vertices of a parallelogram 4 5 9 13 12 7 are the vertices of a hexagon 1 2 3 4 5 are not the vertices of an acceptable figure 47 are not the vertices of an acceptable figure 11 13 23 25 are not the vertices of an acceptable figure


先把的座事先都建好,之後根定的行凸包。
如果不同有,以及角度不是六十度。

#include <stdio.h>
#include <math.h>
#include <sstream>
#include <algorithm>
#define eps 1e-6
using namespace std;
double trix[32768], triy[32768];
const double pi = acos(-1);
struct Pt {
double x, y;
};
bool cmp(Pt a, Pt b) {
if(a.x != b.x)
return a.x < b.x;
return a.y < b.y;
}
double cross(Pt o, Pt a, Pt b) {
return (a.x-o.x)*(b.y-o.y)-(a.y-o.y)*(b.x-o.x);
}
double dist(Pt a, Pt b) {
return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);
}
Pt P[1000], CH[1000];
int convex(int n) {
sort(P, P+n, cmp);
int m = 0, i, t;
for(i = 0; i < n;i++) {
while(m >= 2 && cross(CH[m-2], CH[m-1], P[i]) <= 0)
m--;
CH[m++] = P[i];
}
for(i = n-1, t = m+1; i >= 0; i--) {
while(m >= t && cross(CH[m-2], CH[m-1], P[i]) <= 0)
m--;
CH[m++] = P[i];
}
return m;
}
int main() {
trix[1] = triy[1] = 0;
double disy = sqrt(3)/2;
int i, j, idx = 2, x;
char buf[1024];
for(i = 2; idx < 32768;i++) {
trix[idx] = trix[idx-i+1]-0.5;
triy[idx] = triy[idx-i+1]-disy;
for(j = 1, idx++; j < i && idx < 32768; j++, idx++) {
trix[idx] = trix[idx-1]+1;
triy[idx] = triy[idx-1];
}
}
while(gets(buf)) {
stringstream sin(buf);
int n = 0, m;
while(sin >> x) {
P[n].x = trix[x];
P[n].y = triy[x];
n++;
}
sort(P, P+n, cmp);
m = convex(n);
printf("%s ", buf);
if(m-1 != n || n == 1 || m-1 == 5) {
puts("are not the vertices of an acceptable figure");
continue;
}
double tmp = dist(CH[0], CH[1]);
for(i = 1; i < m; i++) { // check all sides must be of the same length.
if(fabs(dist(CH[i], CH[i-1])-tmp) > eps)
break;
}
if(i != m) {
puts("are not the vertices of an acceptable figure");
continue;
}
for(i = 1; i < m-1; i++) { // check each side must coincide with an edge in the grid.
tmp = fabs(cross(CH[i], CH[i-1], CH[i+1]));
if(fabs(asin(tmp/sqrt(dist(CH[i], CH[i-1]))/sqrt(dist(CH[i], CH[i+1])))*180/pi - 60) > eps)
break;
}
if(i != m-1) {
puts("are not the vertices of an acceptable figure");
continue;
}
printf("are the vertices of ");
if(n == 3)
puts("a triangle");
else if(n == 4)
puts("a parallelogram");
else
puts("a hexagon");
}
return 0;
}

台: Morris
人(789) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][零dp] 242 - Stamps and Envelope Size
此分上一篇:[UVA][第K短路] 10740 - Not the Best

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86