当前位置: 首页 > 工具软件 > marmot > 使用案例 >

Captain Marmot(Codeforces Round #271 div2) C

卞经业
2023-12-01

Captain Marmot
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

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

Hint

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 <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <set>
#include <queue>
#include <stack>
#include <map>
using namespace std;
typedef long long LL;
const int inf=0x3f3f3f3f;
const double pi= acos(-1.0);
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
struct node {
    int x,y;
} rec[10][10],cen[10];
LL dis(struct node a,struct node b)
{
    LL xx,yy;
    LL res;
    xx=(a.x-b.x)*(a.x-b.x);
    yy=(a.y-b.y)*(a.y-b.y);
    res=xx+yy;
    return res;
}
LL bian[6];
//LL xie[2];
void judge()
{
    int i,j,k,l;
    int ans=inf;
    for(i=0; i<4; i++)
        for(j=0; j<4; j++)
            for(k=0; k<4; k++)
                for(l=0; l<4; l++) {
                    bian[0]=dis(rec[i][0],rec[j][1]);
                    bian[1]=dis(rec[j][1],rec[k][2]);
                    bian[2]=dis(rec[k][2],rec[l][3]);
                    bian[3]=dis(rec[l][3],rec[i][0]);
                    bian[4]=dis(rec[i][0],rec[k][2]);
                    bian[5]=dis(rec[j][1],rec[l][3]);
                    //for(int ii=0;ii<6;ii++)
                    //printf("bian[%d]==%d",ii,bian[ii]);
                    sort(bian,bian+6);
                    if(bian[0]==0||bian[1]==0||bian[2]==0||bian[3]==0||bian[4]==0||bian[5]==0)
                        continue;
                    //else if(bian[0]==bian[1]&&bian[1]==bian[2]&&bian[2]==bian[3]&&bian[3]==bian[0]&&xie[0]==xie[1]&&bian[0]*2==xie[0])
                    // ans=min(ans,i+j+k+l);
                    else if(bian[0]==bian[1]&&bian[1]==bian[2]&&bian[2]==bian[3]&&bian[3]==bian[0]&&bian[4]==bian[5]&&bian[0]*2==bian[4])
                        ans=min(ans,i+j+k+l);
                }
    if(ans!=inf)
        printf("%d\n",ans);
    else
        printf("-1\n");

}
int main()
{
    int T,i,j;
    scanf("%d",&T);
    while(T--) {
        for(i=0; i<4; i++) {
            scanf("%d %d",&rec[0][i].x,&rec[0][i].y);
            scanf("%d %d",&cen[i].x,&cen[i].y);
            rec[1][i].x=cen[i].x-(rec[0][i].y-cen[i].y);
            rec[1][i].y=cen[i].y+(rec[0][i].x-cen[i].x);
            rec[2][i].x=cen[i].x-(rec[0][i].x-cen[i].x);
            rec[2][i].y=cen[i].y-(rec[0][i].y-cen[i].y);
            rec[3][i].x=cen[i].x+(rec[0][i].y-cen[i].y);
            rec[3][i].y=cen[i].y-(rec[0][i].x-cen[i].x);
        }
        judge();
    }
    return 0;
}


 类似资料: