codeforces 474C Captain Marmot dfs

傳送門: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;
}


發佈了109 篇原創文章 · 獲贊 5 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章