HDU 4082(计算几何+相似三角形)

问题描述:

Long long ago, in the time of Chinese emperor Yao, ten suns rose into the sky. They burned the crops and scorched the bushes and trees, leaving the people with nothing to eat. 









Hou Yi was the greatest archer at that time. Yao wanted him to shoot down nine suns. Hou Yi couldn't do that job with ordinary arrows. So Yao send him to God to get some super powerful magic arrows. Before Hou Yi left, Yao said to him: "In order to manage our country in a better way, I want to know how many years can I live from now on. Please ask God this question for me." Hou Yi promised him. 
Hou yi came back from God with ten magic arrows. He shot down nine suns, and the world returned to harmony. When Yao asked Hou Yi about the answer of his question, Hou Yi said: "God told me nothing. But I happened to see a 'life and death book' with your name on it. So I know the answer. But you know, I can't tell you because that's God's secret, and anyone who gives out God's secret will be burned by a thunder!" 
Yao was very angry, he shouted: "But you promised me, remember?" Hou Yi said: 
"Ooo-er, let's make some compromise. I can't tell you the answer directly, but I can tell you by my only precious magic arrow. I'll shoot the magic arrow several times on the ground, and of course the arrow will leave some holes on the ground. When you connect three holes with three line segments, you may get a triangle. The maximum number of similar triangles you can get means the number of years you can live from now on." (If the angles of one triangle are equal to the angles of another triangle respectively, then the two triangles are said to be similar.) 
Yao was not good at math, but he believed that he could find someone to solve this problem. Would you help the great ancient Chinese emperor Yao?
Input
There are multiple test cases, and the number of test cases is no more than 12. 
The first line of every test case is an integer n meaning that Hou Yi had shot the magic arrow for n times (2 < n <= 18). 
Then n lines follow. Each line contains two integers X and Y (-100 < X, Y < 100), the coordinate of a hole made by the magic arrow. 
Please note that one hole can be the vertex of multiple triangles. 
The input ends with n = 0.
Output
For each test case, print a line with an integer indicating the maximum number of similar triangles Yao could get.
Sample Input
3
1 1
6 5
12 10
4
0 0
1 1
2 0
1 -1
0
Sample Output
1
4

题目题意:题目给我们n个点,让我们求出这个n个点能构成的三角形中最多有多少对相似三角形。
题目分析:我们先对这n个点处理,求出它能构成的所有的三角形,然后枚举所有的三角形求与它的相似三角形,保存最大值。(判断三角形相似,我用的是角度对应相同)
这里有几个坑点,输入的点有可能是相同的点,如果不同的点少于3个点,那么就直接输出0,然后注意不是任意的三个点都可以构成三角形,共线的不可。

代码如下:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;

const double epx=1e-6;
struct note
{
    int x,y;
    bool operator==(const struct note &a) const {
        return x==a.x&&y==a.y;
    }
}point [30];
int cnt,res;

struct angle//保存一个三角形的角度
{
    double A,B,C;
    bool operator==( const struct angle &a) const {
        return (A-a.A<epx)&&(B-a.B<epx)&&(C-a.C<epx);
    }
}Angle[5000];

bool check(struct note a)//判断这个点以前是否出现过
{
    for (int i=0;i<cnt;i++) {
        if (point[i]==a)
            return false;
    }
    return true;
}
double get_dis(note a,note b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void solve(note a,note b,note c)
{
    bool flag=(b.y-a.y)*(c.x-a.x)==(c.y-a.y)*(b.x-a.x);//是否共线
    if (flag)
        return;
    double dx,dy,dz,p[3];
    dx=get_dis(a,b);
    dy=get_dis(a,c);
    dz=get_dis(b,c);
    p[0]=acos((dx*dx+dy*dy-dz*dz)/(2*dx*dy));//余弦定理求角度
    p[1]=acos((dx*dx+dz*dz-dy*dy)/(2*dx*dz));
    p[2]=acos((dz*dz+dy*dy-dx*dx)/(2*dz*dy));
    sort (p,p+3);//记得排序,因为要对应相等
    Angle[res].A=p[0];
    Angle[res].B=p[1];
    Angle[res].C=p[2];
    res++;
}
int main()
{
    int n;
    while (scanf("%d",&n)!=EOF) {
        if (n==0) break;
        cnt=res=0;
        memset (point,0,sizeof (point));
        for (int i=0;i<n;i++) {
            struct note a;
            scanf("%d%d",&a.x,&a.y);
            if (check(a))//判断一下
                point[cnt++]=a;
        }
        if (cnt<2) { printf("0\n"); continue;}
        for (int i=0;i<cnt-2;i++) {
            for (int j=i+1;j<cnt-1;j++) {
                for (int k=j+1;k<cnt;k++) {
                    solve(point[i],point[j],point[k]);//枚举每三个点
                }
            }
        }
        int ans=0;
        for (int i=0;i<res;i++) {
            int cur=0;
            for (int j=0;j<res;j++) {
                if (Angle[i]==Angle[j])
                    cur++;
            }
            ans=max(ans,cur);
        }
        printf("%d\n",ans);
    }
    return 0;
}
















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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值