2011 Asia Beijing Regional B: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.
2011 Asia Beijing Regional B: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
 

//去年北京现场赛最水的一题了,因为n最多只有18,所以直接暴力解决,只是题目没有说清楚重点的情况,验证是不计算重点的。

//题目没有卡精度,直接利用三角形两个内角相等也顺利的过了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;
#define maxn 6000
#define eps 1e-8

int Fabs(double d)
{
	if(fabs(d)<eps) return 0;
	else return d>0?1:-1;
}

struct point 
{
	double x,y;
	bool operator == (const point &p)
	{
		return Fabs(x-p.x)==0&&Fabs(y-p.y)==0;
	}
}p[20];
vector<double>my[maxn];
map<int,bool>qq;

double x_multi(point p1,point p2,point p3)    
{  
    return (p2.x-p1.x)*(p3.y-p1.y)-(p3.x-p1.x)*(p2.y-p1.y);  
} 

double Dis(point p1,point p2)
{
	return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}

bool similiar(int i,int j)//乘法替代除法比较边长比值全等
{
	return Fabs(my[i][0]*my[j][1]-my[i][1]*my[j][0])==0&&
	       Fabs(my[i][1]*my[j][2]-my[i][2]*my[j][1])==0&&
	       Fabs(my[i][0]*my[j][2]-my[i][2]*my[j][0])==0;
}

int main()
{
	int n,i,j,k;
	while(scanf("%d",&n),n)
	{
		for(i=0;i<maxn;i++) my[i].clear();
		qq.clear();
		int cnt=0,x,y;
		for(i=0;i<n;i++)
		{
			scanf("%d%d",&x,&y);
			if(qq[x*1000+y]==false)
			{
				qq[x*1000+y]=true;
				p[cnt].x=1.0*x,p[cnt].y=1.0*y;
				cnt++;
			}
		}

		int m=0;
		for(i=0;i<cnt;i++)
			for(j=i+1;j<cnt;j++)
				for(k=j+1;k<cnt;k++)
				{
					if(Fabs(x_multi(p[i],p[j],p[k]))==0)
						continue;
					my[m].push_back(Dis(p[i],p[j]));//为了减少精度损失,用边长比值比较好,并且用乘法替代除法
					my[m].push_back(Dis(p[i],p[k]));
					my[m].push_back(Dis(p[j],p[k]));
					sort(my[m].begin(),my[m].end());
					m++;
				}
				
		int sum=0,ans=0;
		for(i=0;i<m;i++)
		{	
			for(j=0,sum=0;j<m;j++)
				if(similiar(i,j)) 
					sum++;
			if(sum>ans) ans=sum;
		}
		/*
		int m=0;
		for(i=0;i<cnt;i++)
			for(j=i+1;j<cnt;j++)
				for(k=j+1;k<cnt;k++)
				{
					if(Fabs(x_multi(p[i],p[j],p[k]))==0)
						continue;
					double Lij=Dis(p[i],p[j]);
					double Lik=Dis(p[i],p[k]);
					double Ljk=Dis(p[j],p[k]);
					double A=(Lij*Lij+Lik*Lik-Ljk*Ljk)/(2.0*Lij*Lik);
					double B=(Ljk*Ljk+Lik*Lik-Lij*Lij)/(2.0*Ljk*Lik);
					double C=(Lij*Lij+Ljk*Ljk-Lik*Lik)/(2.0*Lij*Ljk);
					my[m].push_back(A);//三角函数比较两个内角相等
					my[m].push_back(B);
					my[m].push_back(C);
					sort(my[m].begin(),my[m].end());
					m++;
				}
				
		int sum=0,ans=0;
		bool flag;
		for(i=0;i<m;i++)
		{	
			for(j=0,sum=0;j<m;j++)
			{
				flag=true;
				for(k=0;k<my[j].size()&&flag;k++)
					if(Fabs(my[i][k]-my[j][k])!=0)
						flag=false;
				if(flag) sum++;
			}
			if(sum>ans) ans=sum;
		}
		*/
		
		printf("%d\n",ans);
	}
	return 0;
}



你可能感兴趣的:(Math,struct,Integer,input,each,output)