BZOJ 2592 [Usaco2012 Feb] Symmetry

来源:互联网 发布:游戏作弊器软件 编辑:程序博客网 时间:2024/05/17 22:15

Description

After taking a modern art class, Farmer John has become interested in finding geometric patterns in everything around his farm. He carefully plots the locations of his N cows (2 <= N <= 1000), each one occupying a distinct point in the 2D plane, and he wonders how many different lines of symmetry exist for this set of points. A line of symmetry, of course, is a line across which the points on both sides are mirror images of each-other. Please help FJ answer this most pressing geometric question.

上过现代艺术课后,FJ开始感兴趣于在他农场中的几何图样。他计划将奶牛放置在二维平面上的N个互不相同的点(1<=N<=1000),他希望找出这个点集有多少条对称轴。他急切地需要你帮忙解决这个几何问题。

Input

* Line 1: The single integer N.

* Lines 2..1+N: Line i+1 contains two space-separated integers representing the x and y coordinates of the ith cow (-10,000 <= x,y <= 10,000).

Output

* Line 1: The number of different lines of symmetry of the point set.

Sample Input

4
0 0
0 1
1 0
1 1

Sample Output

4

HINT

Source

Gold

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

计算几何~

我们以1、2号两个点为依据,这两个点有几种情况:两点构成一条对称轴、两点关于一条对称轴对称、其中一点与这两点之外的另一点关于对称轴对称。所以我们先判掉前两种情况,然后O(n)枚举另一个点,共有2*n条对称轴,用set记录一下所有点,判断这条是不是对称轴即可。复杂度O(n^2*logn)。

如果每个点关于直线l的对称点都是已有的点,那么l就是一条对称轴。


#include<cstdio>#include<cmath>#include<set>using namespace std;int n,ans;struct node{double x,y;}a[1001],tmp,tmp1,tmp2;bool equ(double u,double v){return fabs(u-v)<1e-8;}bool operator < (node u,node v){return u.x==v.x ? u.y<v.y:u.x<v.x;}set<node> id;int read(){int x=0,f=1;char ch=getchar();while(ch<'0' || ch>'9') {if(ch=='-') f=-1;ch=getchar();}while(ch>='0' && ch<='9') {x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}return x*f;}node operator + (node u,node v){return (node){u.x+v.x,u.y+v.y};}node operator * (node u,double v){return (node){u.x*v,u.y*v};}bool operator == (node u,node v){return equ(u.x,v.x) && equ(u.y,v.y);}node operator - (node u,node v){return (node){u.x-v.x,u.y-v.y};}double cross(node k,node u,node v){return (u.x-k.x)*(v.y-k.y)-(v.x-k.x)*(u.y-k.y);}void cal(node u,node v,node &xx,node &yy){if(u==v) return;xx=(u+v)*0.5;yy=(node){xx.x+u.y-v.y,xx.y-u.x+v.x};}node line(node a,node b,node c,node d){double u=cross(a,b,c),v=cross(b,a,d);return (node){(c.x*v+d.x*u)/(u+v),(c.y*v+d.y*u)/(u+v)};}node chan(node k,node u,node v){if(u==v) return (node){u.x+u.x-k.x,u.y+u.y-k.y};tmp=(node){k.x+u.y-v.y,k.y-u.x+v.x};return line(k,tmp,u,v)*2-k;}bool chec(node k,node b){for(int i=1;i<=n;i++) {int x=a[i].x,y=a[i].y;if(!id.count(chan(a[i],k,b))) return 0;}return 1;}int main(){n=read();for(int i=1;i<=n;i++) a[i].x=read(),a[i].y=read(),id.insert(a[i]);ans+=chec(a[1],a[2]);cal(a[1],a[2],tmp1,tmp2);ans+=chec(tmp1,tmp2);for(int i=3;i<=n;i++){cal(a[1],a[i],tmp1,tmp2);ans+=chec(tmp1,tmp2);cal(a[2],a[i],tmp1,tmp2);ans+=chec(tmp1,tmp2) && !cross(a[1],tmp1,tmp2);}printf("%d\n",ans);return 0;}


原创粉丝点击