HDU 4082Hou Yi's secret2011北京现场赛B题(统计相似三角形个数)

Hou Yi's secret

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


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.
HDU 4082Hou Yi's secret2011北京现场赛B题(统计相似三角形个数)_第1张图片
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
2011 Asia Beijing Regional Contest
 

              题目大意: 给你n个点,问这些点相似的最多的三角形的个数。

        解题思路:开始看了equal就觉得是全等,WA了一发,然后是因为angle才发现是角度相等TAT就是相似。但还是WA了,然后就猜测是精度的问题,然后就各种精度0.001到10^-12还是WA。因为我判断相似是除法,怕损失了精度。就让博博重新又写了一遍。最后给他写数据的时候。详见代码中3,4组数据。当时博博说过要判重点,自己当时只是测了n=3这样的数据。当时都觉得不是重点的问题,重点是精度的问题。但是后来WA了8次之后终于A了。。

       题目地址:Hou Yi's secret

AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define maxn 50005
#define eps 0.0000001
using namespace std;
int n;

struct mq  //组成三角形三边的比例
{
    double s1;
    double s2;
    double s3;
};
mq tri[10005];

struct mo //输入的点
{
    double a1;
    double a2;
};
mo node[20];

int cmp(mq t1,mq t2)
{
    if(t1.s1<t2.s1) return 1;
    else if(t1.s1==t2.s1&&t1.s2<t2.s2) return 1;
    else if(t1.s1==t2.s1&&t1.s2==t2.s2&&t1.s3<t2.s3) return 1;
    return 0;
}

int main()
{
    int i,p,j,k,q;
    double a,b,c,d,e,f,x,y;
    double m[3];
    while(scanf("%d",&n)&&n)
    {
        p=0;
        q=0;
        scanf("%lf%lf",&node[q].a1,&node[q].a2);
        q++;
        for(i=1;i<n;i++)
        {
           scanf("%lf%lf",&x,&y);
           int flag=0;
           for(j=0;j<q;j++)
           {
               if(x==node[j].a1&&y==node[j].a2)
               {
                   flag=1;
                   break;
               }
           }
           if(!flag)  //点需要判重,详见下面的3,4组测试数据
           {
               node[q].a1=x; node[q].a2=y;
               q++;
           }
        }

        n=q;
        for(i=0;i<n;i++)
            for(j=i+1;j<n;j++)
              for(k=j+1;k<n;k++)
              {
                  a=node[i].a1,b=node[i].a2,c=node[j].a1;
                  d=node[j].a2,e=node[k].a1,f=node[k].a2;
                  m[0]=sqrt((d-b)*(d-b)+(c-a)*(c-a));
                  m[1]=sqrt((f-b)*(f-b)+(e-a)*(e-a));
                  m[2]=sqrt((f-d)*(f-d)+(e-c)*(e-c));
                  sort(m,m+3);
                  if(m[0]+m[1]>m[2])  //可以组成三角形
                  {
                      tri[p].s1=m[1]/m[0];  //各边的比例
                      tri[p].s2=m[2]/m[0];
                      tri[p].s3=m[2]/m[1];
                      p++;
                  }
              }

        sort(tri,tri+p,cmp);  //对边的比例排序
        /*for(i=0;i<p;i++)
            printf("%.12lf %lf %lf\n",tri[i].s1,tri[i].s2,tri[i].s3);
        cout<<p<<endl;*/
        int sum=0;
        int w=1;
        for(i=0;i<p-1;i++)
        {
            if(fabs(tri[i].s1-tri[i+1].s1)<eps&&fabs(tri[i].s2-tri[i+1].s2)<eps&&fabs(tri[i].s3-tri[i+1].s3)<eps)
                 w++;  //三边比例都相同
            else w=1;
            if(sum<w) sum=w;
        }
        if(sum<w) sum=w;
        if(p==0) sum=0;  //没有三角形需要特判
        printf("%d\n",sum);
    }
    return 0;
}

//0MS 264K

/*
6
-1 -1
1 1
1 2
2 2
4 4
4 2
5
0 0
1 0
0 1
2 0
0 2
6
1 1
2 2
3 1
1 3
3 3
2 2
5
1 1
2 2
3 1
1 3
3 3
5
0 0
3 0
100 0
0 3
0 100
*/


你可能感兴趣的:(判重点)