Codeforces Round #271 (Div. 2) C 简单几何



链接:戳这里


C. Captain Marmot
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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).

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.



题意:

给出n*4个点,4个点一一对应一个旋转点a,这四个点每次绕着逆时针旋转90度(可以绕多次),使得四个点可以组成正方形。输出最少的旋转次数。


思路:

对于每个点多可以通过旋转得到新的三个点,加上本身4个,暴力枚举每种情况判断是否能组成正方形

判断能否组成正方形,判断正方形的4条边相等+两条对边是它的根号2倍。这里直接平方不开根号就行了


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#include<bitset>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
#define INF (1ll<<60)-1
#define Max 1e9
using namespace std;
int n;
struct point{
    int x,y;
    point(int x=0,int y=0):x(x),y(y){}
};
typedef point vec;
point operator + (point a,vec b){
    return point(a.x+b.x,a.y+b.y);
}
point operator - (point a,vec b){
    return point(a.x-b.x,a.y-b.y);
}
point rota(point a,point b){
    vec v=a-b;
    vec w=vec(-v.y,v.x);
    return b+w;
}
ll disn(point a,point b){
    ll x=1LL*a.x-1LL*b.x;
    ll y=1LL*a.y-1LL*b.y;
    return 1LL*x*x+1LL*y*y;
}
bool pd(point a,point b,point c,point d){
    ll dis[10];
    dis[0]=disn(a,b);
    dis[1]=disn(b,c);
    dis[2]=disn(c,d);
    dis[3]=disn(d,a);
    dis[4]=disn(a,c);
    dis[5]=disn(b,d);
    sort(dis,dis+6);
    if(dis[0] && dis[0]==dis[3] && dis[4]==dis[5] && dis[0]*2LL==dis[4]) return true;
    return false;
}
point s[110][5][5],a;
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=4;j++){
            scanf("%d%d%d%d",&s[i][j][0].x,&s[i][j][0].y,&a.x,&a.y);
            for(int k=1;k<=3;k++){
                s[i][j][k]=rota(s[i][j][k-1],a);
            }
        }
    }
    for(int i=1;i<=n;i++){
        int ans=Max;
        for(int p1=0;p1<4;p1++){
            for(int p2=0;p2<4;p2++){
                for(int p3=0;p3<4;p3++){
                    for(int p4=0;p4<4;p4++){
                        if(pd(s[i][1][p1],s[i][2][p2],s[i][3][p3],s[i][4][p4]))
                            ans=min(ans,p1+p2+p3+p4);
                    }
                }
            }
        }
        if(ans==Max) cout<<-1<<endl;
        else cout<<ans<<endl;
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值