HDU 4082 Hou Yi's secret --枚举

题意: 给n个点,问最多有多少个相似三角形(三个角对应相等)。

解法: O(n^3)枚举点,形成三角形,然后记录三个角,最后按三个角度依次排个序,算一下最多有多少个连续相等的三元组就可以了。

注意:在同一个坐标的两点只算一次,所以要判一下。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define eps 1e-8
using namespace std;
#define N 100017 struct Point{
double x,y;
Point(double x=, double y=):x(x),y(y) {}
void input() { scanf("%lf%lf",&x,&y); }
};
typedef Point Vector;
int dcmp(double x) {
if(x < -eps) return -;
if(x > eps) return ;
return ;
}
template <class T> T sqr(T x) { return x * x;}
Vector operator + (Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator - (Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator * (Vector A, double p) { return Vector(A.x*p, A.y*p); }
Vector operator / (Vector A, double p) { return Vector(A.x/p, A.y/p); }
bool operator < (const Point& a, const Point& b) { return a.x < b.x || (a.x == b.x && a.y < b.y); }
bool operator >= (const Point& a, const Point& b) { return a.x >= b.x && a.y >= b.y; }
bool operator <= (const Point& a, const Point& b) { return a.x <= b.x && a.y <= b.y; }
bool operator == (const Point& a, const Point& b) { return dcmp(a.x-b.x) == && dcmp(a.y-b.y) == ; }
double Dot(Vector A, Vector B) { return A.x*B.x + A.y*B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) { return (Dot(A, B) / Length(A) / Length(B)); }
double Cross(Vector A, Vector B) { return A.x*B.y - A.y*B.x; } //data segment
struct Tri{
double A[];
Tri(double x,double y,double z)
{ A[] = x, A[] = y, A[] = z; }
Tri(){}
bool operator <(const Tri& a)const
{
if(dcmp(A[]-a.A[]) == )
{
if(dcmp(A[]-a.A[])==) return dcmp(A[]-a.A[])<;
return dcmp(A[]-a.A[])<;
}
return dcmp(A[]-a.A[])<;
}
}t[];
bool operator == (const Tri& a,const Tri& b) { return dcmp(a.A[]-b.A[]) == && dcmp(a.A[]-b.A[]) == && dcmp(a.A[]-b.A[]) == ; }
Point p[];
int tot,n;
//data ends
int mp[][];
int main()
{
int i,j,k;
int a[];
while(scanf("%d",&n)!=EOF && n)
{
memset(mp,,sizeof mp);
tot = ;
int cntt=;
for(i=;i<=n;i++)
{
int a,b;scanf("%d%d",&a,&b);
if(mp[a+][b+]==)
p[cntt].x=a,p[cntt++].y=b;
mp[a+][+b]=;
}
n=cntt-;
for(i=;i<=n;i++)
{
for(j=i+;j<=n;j++)
{
for(k=j+;k<=n;k++)
{
Point A = p[i], B = p[j], C = p[k];
if(A == B || A == C || B == C) continue;
if(dcmp(Cross(B-A,C-A)) == ) continue;
double ang1 = Angle(B-A,C-A);
double ang2 = Angle(A-B,C-B);
double ang3 = Angle(A-C,B-C);
double A1 = min(ang1,min(ang2,ang3));
double A3 = max(ang1,max(ang2,ang3));
double A2 = ang1+ang2+ang3-A1-A3;
t[++tot] = Tri(A1,A2,A3);
}
}
}
sort(t+,t+tot+);
int Maxi = (tot!=), cnt = ;
for(i=;i<=tot;i++)
{
if(t[i] == t[i-])
cnt++;
else
cnt = ;
Maxi = max(Maxi,cnt);
}
cout<<Maxi<<endl;
}
return ;
}
上一篇:21. Merge Two Sorted Lists (Java 合并有序链表 空间复杂度O(1))


下一篇:JS中 submit提交与Form表单里的onsubmit的调用问题?