codeforces 474C Captain Marmot dfs

6 篇文章 0 订阅

传送门:cf 474c

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


        给定四个点的坐标以及它们的旋转中心,每次只能逆时针旋转90度,问这四个点最少旋转多少次能构成一个正方形。


        每个点旋转4次就回到自身状态了,因此加上初始状态就只有4^4种状态,dfs枚举状态判断是否是正方形即可

/******************************************************
 * File Name:   c.cpp
 * Author:      kojimai
 * Creater Time:2014年10月07日 星期二 01时14分52秒
******************************************************/

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
	int a,b,x,y;
}p[5],aa[5];
int ans;
void rotate(int now)
{
	int x = aa[now].y - aa[now].b + aa[now].x;
	int y = aa[now].a - aa[now].x + aa[now].y;
	aa[now].a = x;
	aa[now].b = y;
}
bool judge()
{
	int t,x,y,cnt=0;
	for(int i = 0;i < 4;i++)
	{
		for(int j = i+1;j < 4;j++)
		{
			if(aa[i].a==aa[j].a&&aa[i].b==aa[j].b)
				return false;
			t = (aa[i].a - aa[j].a)*(aa[i].a - aa[j].a) + (aa[i].b - aa[j].b) * (aa[i].b - aa[j].b);
			if(cnt==0)
			{
				cnt = 1;
				x = t;
			}
			else if(cnt == 1)
			{
				if(t != x)
				{
					cnt = 2;
					y = t;
				}
			}
			else if(cnt == 2)
			{
				if(t != x && t != y)
					return false;
			}
		}
	}
	return true;
}
void dfs(int now,int num)
{
	//cout<<"now="<<now<<" num="<<num<<" ans="<<ans<<endl;
	if(num >= ans)
		return;
	if(now==4)
	{
		if(judge())
			ans = num;
		return;
	}
	aa[now] = p[now];
	for(int i=0;i<4;i++)
	{
		if(i)
		{
			rotate(now);
			dfs(now+1,num+i);
		}
		else
			dfs(now+1,num);
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n--)
	{
		for(int i=0;i<4;i++)
			scanf("%d%d%d%d",&p[i].a,&p[i].b,&p[i].x,&p[i].y);
		ans = 16;
		dfs(0,0);
		if(ans == 16)
			ans = -1;	
		cout<<ans<<endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值