codeforces 474c 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, 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 xi, yi, ai, bi ( - 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.


#include <cstdio>
#include <algorithm>
#define LL long long
using namespace std;
struct Point
{
    LL x, y;
    int a, b;
}p[4];
LL d[6];

LL dist(LL x1, LL y1, LL x2, LL y2)
{
    return ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

bool judge(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
    d[0] = dist(x1,y1,x2,y2);
    d[1] = dist(x1,y1,x3,y3);
    d[2] = dist(x1,y1,x4,y4);
    d[3] = dist(x2,y2,x3,y3);
    d[4] = dist(x2,y2,x4,y4);
    d[5] = dist(x3,y3,x4,y4);
    sort(d, d + 6);
    if(d[0] == d[1] && d[1] == d[2] && d[2] == d[3] && d[4] == d[5])
        return true;
    return false;
}

void rev(LL &x, LL &y, int a, int b)
{   
    LL xx = x;
    LL yy = y;
    x = -yy + b + a;
    y = b + xx - a;
}

int cal()
{
    int i, j, k, l;
    bool ok = false;
    int re = 0;
    int mi = 0xfffff;
    for(i = 1; i <= 4; i++)
    {
        int ii = i;
        if(i == 4)
            ii = 0;
        rev(p[0].x, p[0].y, p[0].a, p[0].b);
        for(j = 1; j <= 4; j++)
        {
            int jj = j;
            if(j == 4)
                jj = 0;
            rev(p[1].x, p[1].y, p[1].a, p[1].b);
            for(k = 1; k <= 4; k++)
            {
                int kk = k;
                if(k == 4)
                    kk = 0;
                rev(p[2].x, p[2].y, p[2].a, p[2].b);
                for(l = 1; l <= 4; l++)
                {
                    int ll = l;
                    if(l == 4)
                        ll = 0;
                    rev(p[3].x, p[3].y, p[3].a, p[3].b);
                    if(p[0].x == p[1].x && p[1].x == p[2].x && p[2].x == p[3].x)
                            continue;
                    if(judge(p[0].x,p[0].y,p[1].x,p[1].y,p[2].x,p[2].y,p[3].x,p[3].y))
                    {
                        /*
                        printf("i = %d  j = %d  k = %d  l = %d\n", ii, jj, kk, ll);
                        printf("x0 = %d y0 = %d\n", p[0].x, p[0].y);
                        printf("x1 = %d y1 = %d\n", p[1].x, p[1].y);
                        printf("x2 = %d y2 = %d\n", p[2].x, p[2].y);
                        printf("x3 = %d y3 = %d\n", p[3].x, p[3].y);
                        */
                        re = ii + jj + kk + ll;
                        mi = min(mi, re);
                        ok = true;
                    }
                }
            }
        }
    }
    if(ok)
        return mi;
    else
        return -1;
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
        for(int i = 0; i < 4; i++)
            scanf("%lld %lld %d %d", &p[i].x, &p[i].y, &p[i].a, &p[i].b);
        printf("%d\n", cal());
    }
}


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

你可能感兴趣的:(codeforces)