HDU-4082-Hou Yi's secret

原题
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个点,每个点的坐标都是整数,从这些点中找出三个点组成三角形,并从所有的三角形中拥有相似三角形的最大个数(注意是最大,而不是所有相似三角形的个数,审题很重要,我就是看错了 )。
题解:
这道题目数据范围比较小,最多也只有20个点,可以暴力搜索,先找出所有的三角形,后对于每一个三角形都找出和他相似的三角形个数。
附上AC代码:

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int n,cnt;
const double eps = 1e-8;
struct node
{
    double x,y;//虽然题目是整型,但是后面要计算距离,所以用double还是方便些
}st[20];//定义节点
struct triangle{
    double a[3];
}t[8000];//定义三角形,但储存的是三边长那个,而非三点
bool chline(int a,int b,int c)//判断三点是否共线
{
    return (st[b].x-st[a].x)*(st[c].y-st[a].y)-(st[c].x-st[a].x)*(st[b].y-st[a].y);//ab、bc斜率相等则共线,化简所得
}
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));//计算两点间距离
}
bool similar(triangle t1,triangle t2)//判断两个三角形是否相似
{
    if( (fabs(t1.a[0]*t2.a[1]-t1.a[1]*t2.a[0])<eps)&&
        (fabs(t1.a[1]*t2.a[2]-t1.a[2]*t2.a[1])<eps)&&
        (fabs(t1.a[0]*t2.a[2]-t1.a[2]*t2.a[0])<eps))//根据三边对应成比例,化简所得
    return true;
    return false;
}
int main()
{
    while(cin>>n,n)//若n==0,退出
    {
        cnt=0;//初始化
        for(int i=1;i<=n;i++)
        {
            cin>>st[i].x>>st[i].y;
            for(int j=1;j<i;j++)
            {
                if(st[i].x==st[j].x&&st[i].y==st[j].y)//如果已经有重复的点,则去掉,只保留一个点
                {
                    i--;n--;
                    break;
                }
            }
        }
        if(n==3&&chline(1,2,3)){cout<<1<<endl;continue;}//题目说n>2,若是只有三个点且不共线则只可能有一个相似三角形(它本身)
        for(int i=1;i<=n-2;i++){
            for(int j=i+1;j<=n-1;j++){
                for(int k=j+1;k<=n;k++){
                    if(chline(i,j,k)){
                        t[cnt].a[0]=dis(st[i],st[j]);
                        t[cnt].a[1]=dis(st[i],st[k]);
                        t[cnt].a[2]=dis(st[j],st[k]);
                        sort(t[cnt].a,t[cnt].a+3);//对边长进行排序,方便比较对应成比例(一定是小的对应小的)
                        cnt++;
                    }
                }
            }
        }
        int ans = 0;
        for(int i=0;i<cnt;i++){
            int tmp = 1;
            for(int j=i+1;j<cnt;j++)
                if(similar(t[i],t[j]))
                    tmp++;
            ans=max(ans,tmp);//取较大值
        }
        cout<<ans<<endl;
    }
    return 0;
}

还有我之前写的一种方法,但是WA了
WA code(可以帮我找下错误):

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
int n;
int vis[20][20][20];//记录由ijk三点构成的三角形的相似三角形的个数
struct node
{
    int x,y;
}st[20];
bool chline(int a,int b,int c)//判断三点是否共线
{
    double k1,k2;
    if(st[a].x==st[b].x)k1=1e5;
    else k1=((st[a].y-st[b].y)/(st[a].x-st[b].x));
    if(st[a].x==st[c].x)k2=1e5;
    else k2=((st[a].y-st[c].y)/(st[a].x-st[c].x));
    if(k1==k2)return false;
    else return true;//不共线返回true
}
double cosx(node a,node b,node c)
{
    double lc=(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y);//ab
    double lb=(a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y);//ac
    double la=(c.x-b.x)*(c.x-b.x)+(c.y-b.y)*(c.y-b.y);//bc
    return (lb+lc-la)/(2*sqrt(lb)*sqrt(lc));//返回cosA
}
bool check(node a,node b,node c,node d,node e,node f)//通过cos值来判断
{
    double fi[3],se[3];//记录每个角的cos值
    fi[0]=cosx(a,b,c);
    fi[1]=cosx(b,c,a);
    fi[2]=cosx(c,a,b);
    se[0]=cosx(d,e,f);
    se[1]=cosx(e,f,d);
    se[2]=cosx(f,d,e);
    sort(fi,fi+3);//对于cos值从小到大排序来一一对应
    sort(se,se+3);
    for(int i=0;i<3;i++)
    {
        if(fi[i]!=se[i])
            return false;//如果不对应则不相似
    }
    return true;
}
int main()
{
    while(cin>>n,n)
    {
        memset(vis,0,sizeof(vis));//初始化
        for(int i=1;i<=n;i++)
        {
            cin>>st[i].x>>st[i].y;
        }
        if(n==3&&chline(1,2,3)){cout<<1<<endl;continue;}//如果只有三个点且不共线则只有一个相似三角形
        for(int i=1;i<=n-2;i++)
            for(int j=i+1;j<=n-1;j++)
                for(int k=j+1;k<=n;k++)//遍历(i,j,k)三点构成的三角形
                    for(int a=1;a<=n-2;a++)
                        for(int b=a+1;b<=n-1;b++)
                            for(int c=b+1;c<=n;c++)//遍历(a,b,c)三点构成的三角形
        {
            if(chline(i,j,k)&&chline(a,b,c))
            {
                if(check(st[i],st[j],st[k],st[a],st[b],st[c]))
                   {
                       vis[i][j][k]++;//如果(i,j,k)和(a,b,c)相似则在(i,j,k)的相似三角形个数上加一
                   }
            }
        }
        int ans=0;//初始化为0
        for(int i=1;i<=n-2;i++)
            for(int j=i+1;j<=n-1;j++)
                for(int k=j+1;k<=n;k++)
                {
                    ans=ans>vis[i][j][k]?ans:vis[i][j][k];//找出相似三角形最多的个数
                }
        cout<<ans<<endl;
    }
    return 0;
}

欢迎评论!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值