Codeforces Round #271 (Div. 2) C. Captain Marmot

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, yi90 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

思路:枚举旋转以后的所以情况,判断是否组成正方形

判断是否为正方形,我们可以枚举1和谁是对角,

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<ctime>
#include<bitset>
#define LL long long
#define INF 89898989
#define mod 1000000007
#define maxn 100010
#define pi acos(-1.0)
#define eps 1e-6
using namespace std;

const double e = 0.5*pi;
struct node
{
   double x,y ;
};
node get(node a,node b )
{
    node bb ;
    bb.x = ((double)a.x-b.x)*cos(e)-(a.y-b.y)*sin(e)+b.x;
    bb.y = ((double)a.y-b.y)*cos(e)+(a.x-b.x)*sin(e)+b.y;
    return bb ;
}
int ans=INF;
node a[7],b[7],aa[7] ;
double dis(int i,int j )
{
    return (a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
}
void out()
{
     for( int i = 1 ; i <= 4 ;i++)
     {
         printf("%.5lf %.5lf
",a[i].x,a[i].y) ;
     }
     puts("=====");
}
bool find(int i,int j )
{
    if(i==1&&j==2)
    {
        if(fabs(dis(1,2)-dis(3,4)) <= eps && fabs(dis(1,3)-dis(3,2)) <= eps ){
                if(fabs(dis(1,4)-dis(4,2)) <= eps)
                return true;
        }
    }
    if(i==1&&j==3)
    {
         if(fabs(dis(1,3)-dis(2,4)) <= eps && fabs(dis(1,2)-dis(2,3)) <= eps )
                if(fabs(dis(1,4)-dis(4,3)) <= eps)
                return true;
    }
    if(i==1&&j==4)
    {
         if(fabs(dis(1,4)-dis(3,2)) <= eps && fabs(dis(1,2)-dis(2,4)) <= eps )
          if(fabs(dis(1,3)-dis(4,3)) <= eps)
                return true;
    }
    return false;
}
bool check()
{
    for( int i = 1 ; i < 2 ;i++)
    {
        for(int j = i+1 ; j <= 4 ;j++)
        {
            if(find(i,j)&&dis(i,j)>eps)return true;
        }
    }
    return false;
}
void dfs(int n,int cnt)
{
    if(n>=5)
    {
        bool flag=check();
        if(flag)
        {
            ans = min(ans,cnt) ;
        }
        return ;
    }
    for(int i = 0 ; i < 4 ;i++)
    {
        a[n]=aa[n];
        for(int j =1 ; j<=i;j++)
        {
            a[n]=get(a[n],b[n]);
        }
        dfs(n+1,i+cnt);
    }
}
int main()
{
    int i , n , k, m , j ;
    int T;
    cin >>n ;
    while(n--)
    {
        for( i = 1 ; i <= 4 ;i++){
            //scanf("%lf%lf",&aa[i].x,&aa[i].y) ;
            cin >> aa[i].x >> aa[i].y ;
            cin >> b[i].x >> b[i].y ;
          //  scanf("%lf%lf",&b[i].x,&b[i].y) ;
            a[i]=aa[i];
        }
        ans=INF;
        for( i = 0 ; i < 4 ;i++ )
        {
            a[1]=aa[1];
            for( j =1 ; j<=i;j++)
            {
                a[1] = get(a[1],b[1]);
            }
            dfs(2,i) ;
        }
        if(ans==INF)ans=-1;
        printf("%d
",ans);
    }
    return  0;
}

  

原文地址:https://www.cnblogs.com/20120125llcai/p/4009088.html