Codeforces 474 C. Captain Marmot


4*4*4*4暴力+点的旋转+推断正方型

C. Captain Marmot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Captain Marmot wants to prepare a huge and important battle against his enemy, Captain Snake. For this battle he has n regiments, each consisting of 4 moles.

Initially, each mole i (1 ≤ i ≤ 4n) is placed at some position (xi, yi) in the Cartesian plane. Captain Marmot wants to move some moles to make the regiments compact, if it's possible.

Each mole i has a home placed at the position (ai, bi). Moving this mole one time means rotating his position point (xi, yi) 90 degrees counter-clockwise around it's home point (ai, bi).

A regiment is compact only if the position points of the 4 moles form a square with non-zero area.

Help Captain Marmot to find out for each regiment the minimal number of moves required to make that regiment compact, if it's possible.

Input

The first line contains one integer n (1 ≤ n ≤ 100), the number of regiments.

The next 4n lines contain 4 integers xiyiaibi ( - 104 ≤ xi, yi, ai, bi ≤ 104).

Output

Print n lines to the standard output. If the regiment i can be made compact, the i-th line should contain one integer, the minimal number of required moves. Otherwise, on the i-th line print "-1" (without quotes).

Sample test(s)
input
4
1 1 0 0
-1 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-2 1 0 0
-1 1 0 0
1 -1 0 0
1 1 0 0
-1 1 0 0
-1 1 0 0
-1 1 0 0
2 2 0 1
-1 0 0 -2
3 0 0 -2
-1 1 -2 0
output
1
-1
3
3
Note

In the first regiment we can move once the second or the third mole.

We can't make the second regiment compact.

In the third regiment, from the last 3 moles we can move once one and twice another one.

In the fourth regiment, we can move twice the first mole and once the third mole.



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>

using namespace std;

const double pi=3.1415926/2.;
const int INF=0x3f3f3f3f;

int n;
struct PT
{
    double x,y,hx,hy;
}pt[10][10];

struct Point
{
    int x,y;
}A,B,C,D;

int isSQRT(Point a,Point b,Point c,Point d)
{
    int i,j,sumt=0,sumo=0;
    double px[10],py[10];//代表6条边的 向量
    double y[10];
    memset(y,0,sizeof(y));
    px[1]=a.x-b.x;
    py[1]=a.y-b.y;
    px[2]=a.x-c.x;
    py[2]=a.y-c.y;
    px[3]=a.x-d.x;
    py[3]=a.y-d.y;
    px[4]=b.x-c.x;
    py[4]=b.y-c.y;
    px[5]=b.x-d.x;
    py[5]=b.y-d.y;
    px[6]=c.x-d.x;
    py[6]=c.y-d.y;
    for(i=1; i<=6; i++)
    {
        for(j=i+1; j<=6; j++)
            if((px[i]*px[j]+py[i]*py[j])==0)//推断垂直
            {
                y[i]++;
                y[j]++;
            }
    }
    for(i=1; i<=6; i++)
    {
        if(y[i]==2)
            sumt++;//有2条边 与其垂直的个数
        if(y[i]==1)
            sumo++;//有1条边 与其垂直的个数
    }
    if(sumt==4&&sumo==2)
        return 1;// 是正方形
    if(sumt==4)
        return 0;//是 矩形
    return 0;//都不是
}

int main()
{
    scanf("%d",&n);
    while(n--)
    {
        for(int i=0;i<4;i++)
        {
            double a,b,c,d;
            scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
            pt[i][0].x=a,pt[i][0].y=b;
            pt[i][0].hx=c,pt[i][0].hy=d;
            ///....
            for(int j=1;j<4;j++)
            {
                pt[i][j].x=pt[i][j-1].x;
                pt[i][j].y=pt[i][j-1].y;
                pt[i][j].hx=pt[i][j-1].hx;
                pt[i][j].hy=pt[i][j-1].hy;
                ///x0= (x - rx0)*cos(a) - (y - ry0)*sin(a)  + rx0 ;
                ///y0= (x - rx0)*sin(a) + (y - ry0)*cos(a) + ry0 ;
                pt[i][j].x=(pt[i][j-1].x-pt[i][j-1].hx)*0-(pt[i][j-1].y-pt[i][j-1].hy)*1+pt[i][j-1].hx;
                pt[i][j].y=(pt[i][j-1].x-pt[i][j-1].hx)*1+(pt[i][j-1].y-pt[i][j-1].hy)*0+pt[i][j-1].hy;

            }
        }

        int ans=INF;
        for(int i1=0;i1<4;i1++)
        {
            for(int i2=0;i2<4;i2++)
            {
                for(int i3=0;i3<4;i3++)
                {
                    for(int i4=0;i4<4;i4++)
                    {
                        int temp=i1+i2+i3+i4;
                        if(temp>ans) continue;
                        A.x=pt[0][i1].x;A.y=pt[0][i1].y;
                         B.x=pt[1][i2].x;B.y=pt[1][i2].y;
                          C.x=pt[2][i3].x;C.y=pt[2][i3].y;
                           D.x=pt[3][i4].x;D.y=pt[3][i4].y;
                        if(isSQRT(A,B,C,D)==true)
                            ans=min(ans,temp);
                    }
                }
            }
        }

        if(ans==INF) ans=-1;
        printf("%d
",ans);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/zhchoutai/p/6937294.html