CodeForces 474C(点绕点旋转后坐标公式,判断四个点正方形存距离平方比1:2)

 1 #include<stdio.h>
 2 #include<string.h>
 3 int i,j,k,f,flag,c[300],s[300],t,x[6],y[6],a[6],b[6],mini,cnt;
 4 long long temp;
 5 int aa,bb,cc,dd1;
 6 long long  tempx[10],tempy[10],dd[5];
 7 int main()
 8 {
 9     c[0]=1; c[90]=0; c[180]=-1; c[270]=0;
10     s[0]=0; s[90]=1; s[180]=0; s[270]=-1;
11     scanf("%d",&t);
12     while (t--)
13     {
14         for (i=1;i<=4;i++) scanf("%d%d%d%d",&x[i],&y[i],&a[i],&b[i]);
15         mini=100;
16         for (i=0;i<=3;i++)
17             for (j=0;j<=3;j++)
18              for (k=0;k<=3;k++)
19               for (f=0;f<=3;f++)
20         {
21             if (i+j+k+f>mini) continue;
22             //x'-x0=(x-x0)cosa-(y-y0)sina
23            //y'-y0=(x-x0)sina+(y-y0)cosa
24             tempx[1]=(x[1]-a[1])*c[i*90]-(y[1]-b[1])*s[i*90]+a[1];
25             tempy[1]=(x[1]-a[1])*s[i*90]+(y[1]-b[1])*c[i*90]+b[1];
26 
27             tempx[2]=(x[2]-a[2])*c[j*90]-(y[2]-b[2])*s[j*90]+a[2];
28             tempy[2]=(x[2]-a[2])*s[j*90]+(y[2]-b[2])*c[j*90]+b[2];
29 
30             tempx[3]=(x[3]-a[3])*c[k*90]-(y[3]-b[3])*s[k*90]+a[3];
31             tempy[3]=(x[3]-a[3])*s[k*90]+(y[3]-b[3])*c[k*90]+b[3];
32 
33             tempx[4]=(x[4]-a[4])*c[f*90]-(y[4]-b[4])*s[f*90]+a[4];
34             tempy[4]=(x[4]-a[4])*s[f*90]+(y[4]-b[4])*c[f*90]+b[4];
35             cnt=0;
36             for (int ii=1;ii<=4;ii++)
37                 for (int jj=ii+1;jj<=4;jj++)
38             {
39                 temp=(tempx[ii]-tempx[jj])*(tempx[ii]-tempx[jj])+(tempy[ii]-tempy[jj])*(tempy[ii]-tempy[jj]);
40                 if (cnt<=2)
41                 {
42                     flag=0;
43                     for (int kk=1;kk<=cnt;kk++) if (dd[kk]==temp) flag=kk;
44                     if (flag==0) {cnt++; dd[cnt]=temp; }
45                 }
46             }
47             if (cnt==2)
48             {
49                 if (dd[1]>dd[2]) {temp=dd[1]; dd[1]=dd[2]; dd[2]=temp; }
50                 if (dd[2]==2*dd[1]) mini=i+j+k+f;
51             }
52         }
53         if (mini==100) printf("-1\n"); else printf("%d\n",mini);
54     }
55 }

旋转公式:

x'-x0=(x-x0)cosa-(y-y0)sina
y'-y0=(x-x0)sina+(y-y0)cosa

http://codeforces.com/contest/474/problem/C

posted on 2014-10-07 18:39  xiao_xin  阅读(276)  评论(0编辑  收藏  举报

导航