hdu4082 求相似三角形,模拟题

A了4081顺便看了一下4082,感觉题目不好翻译,前几段都是没用的,意思就是给你n个点(0<n<18),数据范围很小…

求这n个点能组成最多有多少个相似三角形。

暴力枚举,但是要注意很多细节,首先:

①去重点,在图论里总是会有去掉重边,可是计算几何里很少遇到去掉重点的问题,所以容易被遗忘,so……记住!!!!

②判断三个点能否组成三角形,即是否共线。

后台数据应该水了,一开始的代码,对于

4

1 1

2 2

3 3

4 4

这组数据输出1也AC了~

View Code
 1 #include <stdio.h>
 2 #include <iostream>
 3 #include <math.h>
 4 #include <algorithm>
 5 #define eps 1e-8
 6 using namespace std;
 7 struct point{
 8     int x,y;
 9 }p[25];
10 struct triangle{
11     double a,b,c;
12 }t[410];
13 int cross(point p0,point p1,point p2){
14     return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
15 }
16 double dist(point p1,point p2){
17     return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
18 }
19 bool similar(triangle t1,triangle t2){
20     if( (fabs(t1.a*t2.b-t1.b*t2.a)<eps)&&
21         (fabs(t1.b*t2.c-t1.c*t2.b)<eps)&&
22         (fabs(t1.a*t2.c-t1.c*t2.a)<eps) )
23     return 1;
24     return 0;
25 }
26 int main(){
27     int n;
28     while(scanf("%d",&n)!=EOF&&n){
29         for(int i=0;i<n;i++){
30             scanf("%d%d",&p[i].x,&p[i].y);
31             for(int j=0;j<i;j++){
32                 if(p[i].x==p[j].x&&p[i].y==p[j].y){
33                     i--;n--;
34                     break;
35                 }
36             }
37         }
38         if(n<3){
39             printf("0\n");
40             continue;
41         }
42         int top=0;
43         double d[3];
44         for(int i=0;i<n;i++){
45             for(int j=i+1;j<n;j++){
46                 for(int k=j+1;k<n;k++){
47                     if(cross(p[i],p[j],p[k])!=0){
48                         d[0]=dist(p[i],p[j]);
49                         d[1]=dist(p[i],p[k]);
50                         d[2]=dist(p[k],p[j]);
51                         sort(d,d+3);
52                         t[top].a=d[0];
53                         t[top].b=d[1];
54                         t[top].c=d[2];
55                         top++;
56                     }
57                 }
58             }
59         }
60         if(top==0){
61             printf("0\n");
62             continue;
63         }
64         int _max=0;
65         for(int i=0;i<top;i++){
66             int res=0;
67             for(int j=i+1;j<top;j++){
68                 if(similar(t[i],t[j])) res++;
69             }
70             _max=max(res,_max);
71         }
72         printf("%d\n",_max+1);
73     }
74     return 0;
75 }

 

posted @ 2013-01-11 00:06  _sunshine  阅读(584)  评论(0编辑  收藏  举报