HDU 4082 Hou Yi's secret

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4082


题面:

Hou Yi's secret

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


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 4082 Hou Yi's secret_第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


题目大意:

    给定一些点,问这些点,最多可以组成多少相似三角形。


解题:

   因为最大只有18个点,C(18,3)好像是816,直接暴力枚举三个点,构成三角形就行。然后比较三角形,判断是否相似。


坑点:

   1.有重点(其实应该注意)

   2.三点共线。

   3.问的是一组相似三角形最多有多少。


代码:

#include <iostream>
#include <cstdio>
#include <cmath>
#define eps 1e-5
using namespace std;
bool sign;
struct triangle
{
	double maxn,mid,minn;
	bool flag;
}store[1000];
int x[20],y[20];
double dis(int xa,int ya,int xb,int yb)
{
	return sqrt(1.0*(xa-xb)*(xa-xb)+(ya-yb)*(ya-yb));
}
double max(double a,double b)
{
	return a>b?a:b;
}
double min(double a,double b)
{
	return a<b?a:b;
}
int max(int a,int b)
{
	return a>b?a:b;
}
triangle cal(int xa,int ya,int xb,int yb,int xc,int yc)
{
	triangle res;
	if((yb-ya)*(xc-xb)-(xb-xa)*(yc-yb)==0)
	{
		res.flag=false;
		return res;
	}
	else
	{
		res.flag=true;
		sign=true;
        double a,b,c,t;
		a=dis(xa,ya,xb,yb);
		b=dis(xa,ya,xc,yc);
		c=dis(xb,yb,xc,yc);
        t=a+b+c;
		res.maxn=max(a,max(b,c));
		res.minn=min(a,min(b,c));
		res.mid=t-res.maxn-res.minn;
		return res;
	}
}
int main()
{
    int n,cnt,ans,temp,m;
	double a,b,c;
	bool flag;
	while(scanf("%d",&n)&&n)
	{
	   cnt=0;
	   m=1;
       for(int i=1;i<=n;i++)
		   scanf("%d%d",&x[i],&y[i]);
       for(int i=1;i<=n;i++)
	   {
		  sign=true;
          for(int j=1;j<m;j++)
		  {
             if(x[i]==x[j]&&y[i]==y[j])
			 {
				 sign=false;
				 break;
			 }
		  }
		  if(sign)
		  {
			  x[m]=x[i];
			  y[m]=y[i];
			  m++;
		  }
	   }
	   for(int i=1;i<m-2;i++)
	   {
		   for(int j=i+1;j<m-1;j++)
		   {
			   for(int k=j+1;k<m;k++)
			   {
                  store[cnt++]=cal(x[i],y[i],x[j],y[j],x[k],y[k]);
			   }
		   }
	   }
	   ans=0;
	   for(int i=0;i<cnt;i++)
	   {
		   if(store[i].flag)
		   {
			   temp=1;
			   for(int j=i+1;j<cnt;j++)
			   {
				   if(store[j].flag)
				   {
                       a=store[i].maxn/store[j].maxn;
					   b=store[i].mid/store[j].mid;
					   c=store[i].minn/store[j].minn;
					   if(fabs(a-b)<eps&&fabs(b-c)<eps)
					   {
						   temp++;
						   flag=true;
					   }
				   }
			   }
               ans=max(ans,temp);
		   }
	   }
       printf("%d\n",ans);
	}
	return 0;
}



你可能感兴趣的:(入门,HDU)