Captain Marmot CodeForces - 474C 暴力搜索+计算几何

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).

Examples

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.

 

题意:给你四个点以及每个点的中心点(即绕这个点逆时针旋转),每个点可以绕其本身的中心点逆时针旋转,每次可以转90度,问 要使得这四个点组成一个正方形,求最少的旋转次数,如果怎么旋转都不能组成正方形,输出-1;

思路:对四个点进行旋转的情况都枚举一下,找一个最小的次数,如果找不到就输出-1;

ps:一个点绕另一个点逆时针旋转  t  度公式:

        xx=x2+(x1-x2)*cos(t)-(y1-y2)*sin(t);
        yy=y2+(y1-y2)*cos(t)+(x1-x2)*sin(t); 

代码如下:

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
const int maxn=1e6+5;
typedef long long ll;
int n,m,ans;
const double PI=acos(-1);
const int INF=0x3f3f3f3f;
struct node
{
    int x,y,x1,y1;
}pos[5],pos1[5],pos2[5];
bool cmp(node a,node b)
{
    if(a.x1!=b.x1)
        return a.x1b.y1;
}
int dis(int xx1,int yy1,int xx2,int yy2)
{
    int xx=xx1-xx2;
    int yy=yy1-yy2; 
    return xx*xx+yy*yy; 
}
bool is_square()
{
    for(int i=0;i<4;i++)
        pos[i].x1=pos2[i].x1,pos[i].y1=pos2[i].y1;
    sort(pos,pos+4,cmp);
    if(pos[1].x1==pos[2].x1&&pos[1].y1==pos[2].y1)
        return 0; 
    int d1=dis(pos[0].x1,pos[0].y1,pos[1].x1,pos[1].y1);
    int d2=dis(pos[1].x1,pos[1].y1,pos[3].x1,pos[3].y1);
    int d3=dis(pos[2].x1,pos[2].y1,pos[3].x1,pos[3].y1);
    int d4=dis(pos[0].x1,pos[0].y1,pos[2].x1,pos[2].y1);
    int d5=dis(pos[0].x1,pos[0].y1,pos[3].x1,pos[3].y1);
    int d6=dis(pos[1].x1,pos[1].y1,pos[2].x1,pos[2].y1); 
    if(d1==d2&&d2==d3&&d3==d4&&d1!=0&&d5==d6&&d5!=0)
        return 1;
    return 0;
}
void dfs(int step,int cnt)
{
    if(cnt==4)
    {
        if(is_square()) 
            ans=min(ans,step); 
        return ;
    }
    for(int i=0;i<4;i++)
    {
        int x1=pos1[cnt].x1;
        int y1=pos1[cnt].y1;
        int x2=pos1[cnt].x;
        int y2=pos1[cnt].y; 
        double b=i*PI/2;
        int co=cos(b);
        int si=sin(b);
        pos2[cnt].x1=x2+(x1-x2)*co-(y1-y2)*si;
        pos2[cnt].y1=y2+(y1-y2)*co+(x1-x2)*si; 
        dfs(step+i,cnt+1);
    }
}
int judge()
{
    ans=INF;
    dfs(0,0);
    if(ans==INF)
        return -1;
    return ans;
}
int main()
{ 
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<4;j++)
            scanf("%d%d%d%d",&pos1[j].x1,&pos1[j].y1 ,&pos1[j].x ,&pos1[j].y );
        printf("%d\n",judge());
    }
    return 0;
}

 

你可能感兴趣的:(Captain Marmot CodeForces - 474C 暴力搜索+计算几何)