HDU - 4082 Hou Yi‘s secret (计算几何)

点击打开题目链接

Hou Yi's secret

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4931    Accepted Submission(s): 1120

Problem Description

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

Source

Recommend

lcy   |   We have carefully selected several similar problems for you:   4081  4085  4089  4090  4087 

Statistic | Submit | Discuss | Note

题目大意:给出n个点,连接成三角形,求与某个三角形相似的三角形的最大个数。

思路:遍历所有的点,如果三个点能构造成三角形则放入vector数组里,然后遍历vector数组求最大解

比赛时WA了5发才A掉,看似简单但AC率极低的一个题。主要坑点有两个。

①判断是否可以构成三角形,只判断两边之和大于第三边是不够的,因为可能有平行的情况,但是判断斜率的话又太麻烦,还有考虑斜率不存在。所以可以直接用向量法判平行,然后判断两边之和大于三边。

②点可以重复。所以要先进行去重,用了set。

今天跟队友学到很多,发现了很多自己的不足。比如我做题太心急,WA掉多次之后就没办法整理出新的思路,还有就是代码没注释没空格,不方便维护检查程序。另外做题要认真读题意,不能先入为主想当然的做。

附上AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
#include<cmath>
#include<set>

using namespace std;
set<pair<double , double> >s;
int _max,ans;
int flag=1;

struct tris{
    double x,y;
}tri[30];

struct angle{
    double a1,a2,a3;
};
vector<angle>v;
int main(){
    int n;
    while(~scanf("%d",&n)&&n!=0){
        v.clear();
        s.clear();
        for(int i=1;i<=n;i++){
            double t1,t2;
            scanf("%lf%lf",&t1,&t2);
            pair<double ,double >p;
            p.first=t1;
            p.second=t2;
            s.insert(p);
        }
        int cnt=1;
        for(set<pair<double,double> >::iterator it=s.begin();it!=s.end();it++){
            tri[cnt].x=it->first;
            tri[cnt++].y=it->second;
        }
        for(int i=1;i<cnt;i++){
            for(int j=i+1;j<cnt;j++){
                for(int k=j+1;k<cnt;k++){
                        //求三角形三边
                        double n1,n2,n3;
                        double sum1=0.00;
                        n1=sqrt((tri[i].x-tri[j].x)*(tri[i].x-tri[j].x) + (tri[i].y-tri[j].y)*(tri[i].y-tri[j].y));
                        n2=sqrt((tri[i].x-tri[k].x)*(tri[i].x-tri[k].x) + (tri[i].y-tri[k].y)*(tri[i].y-tri[k].y));
                        n3=sqrt((tri[k].x-tri[j].x)*(tri[k].x-tri[j].x) + (tri[k].y-tri[j].y)*(tri[k].y-tri[j].y));
                        //边排序
                        sum1=n1+n2+n3;
                        n1=min(n1,min(n2,n3));
                        n3=max(n1,max(n2,n3));
                        n2=sum1-n1-n3;
                        //三个向量
                        double x1,y1,x2,y2,x3,y3;
                        x1=tri[i].x-tri[j].x;
                        y1=tri[i].y-tri[j].y;
                        x2=tri[j].x-tri[k].x;
                        y2=tri[j].y-tri[k].y;
                        x3=tri[i].x-tri[k].x;
                        y3=tri[i].y-tri[k].y;
                        //判断向量平行
                        double k1=(x1*y2)-(x2*y1);
                        double k2=(x2*y3)-(x3*y2);
                        double k3=(x1*y3)-(x3*y1);
                        if(fabs(k1)<=1e-9||fabs(k2)<=1e-9||fabs(k3)<=1e-9)continue;
                        if(n1+n2-n3<=1e-9)continue;
                        //求三角形三角的cos值
                        angle tmp;
                        tmp.a1=(n2*n2+n3*n3-n1*n1)/(2.0*n2*n3);
                        tmp.a2=(n1*n1+n3*n3-n2*n2)/(2.0*n1*n3);
                        tmp.a3=(n1*n1+n2*n2-n3*n3)/(2.0*n1*n2);
                        //排序
                        double sum=tmp.a1+tmp.a2+tmp.a3;
                        tmp.a1=min(tmp.a1,min(tmp.a2,tmp.a3));
                        tmp.a3=max(tmp.a1,max(tmp.a2,tmp.a3));
                        tmp.a2=sum-tmp.a1-tmp.a3;
                        v.push_back(tmp);
                }
                flag=1;
                if(v.size()==0)flag=0;
               _max=0,ans=0;
                for(int i=0;i<v.size();i++){
                    for(int j=0;j<v.size();j++){
                        if(fabs(v[i].a1-v[j].a1)<=1e-9 && fabs(v[i].a2-v[j].a2)<=1e-9 && fabs(v[i].a3-v[j].a3)<=1e-9){
                            _max++;
                        }
                    }
                    ans=max(_max,ans);
                    _max=0;
                }
            }
        }
        if(flag==0)printf("0\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chook_lxk

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值