您的位置:首页 > 大数据 > 人工智能

CodeForces 474C - Captain Marmot

2016-07-24 14:54 579 查看
题意:每组共4个点,4个点分别有起始坐标和轴心坐标,每个点每次都可以绕自己的轴心坐标90°,问能否用最少次数内4个点组成一个正方形,若能则输出最少次数,否则输出-1。

每个点有4个方位可以选择,共4个点,4 * 4 * 4 * 4 = 256种,暴力即可

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
typedef long long ll;
typedef unsigned long long llu;
const int MAXN = 100 + 10;
const int MAXT = 10000 + 10;
const int INF = 0x7f7f7f7f;
const double pi = acos(-1.0);
const double eps = 1e-6;
using namespace std;

struct Point{
int x, y;
bool operator < (const Point &b) const{
return x < b.x || (y < b.y && x == b.x);
}
Point(int a = 0, int b = 0) : x(a), y(b) {}
Point(const Point &b) : x(b.x), y(b.y) {}
Point& operator = (const Point &b){
x = b.x; y = b.y;
return *this;
}
}p[5];

int xx[5], yy[5], a[5], b[5];

Point f1(int x, int y, int ox, int oy){
return Point(x, y);
}

Point f2(int x, int y, int ox, int oy){
return Point(ox + oy - y, oy - ox + x);
}

Point f3(int x, int y, int ox, int oy){
return Point(2 * ox - x, 2 * oy - y);
}

Point f4(int x, int y, int ox, int oy){
return Point(y - oy + ox, ox - x + oy);
}

bool judge(){
sort(p, p + 4);
int c1x = p[1].x - p[0].x;
int c1y = p[1].y - p[0].y;
int c1 = c1x * c1x + c1y * c1y;

int c2x = p[2].x - p[0].x;
int c2y = p[2].y - p[0].y;
int c2 = c2x * c2x + c2y * c2y;

int c3x = p[3].x - p[2].x;
int c3y = p[3].y - p[2].y;
int c3 = c3x * c3x + c3y * c3y;

int c4x = p[3].x - p[1].x;
int c4y = p[3].y - p[1].y;
int c4 = c4x * c4x + c4y * c4y;

if(c1 == c2 && c2 == c3 && c3 == c4){
if(c1x * c2x + c1y * c2y == 0 && c2x * c3x + c2y * c3y == 0 && c3x * c4x + c3y * c4y == 0 && (p[0].x != p[1].x || p[0].y != p[1].y))
return true;
}
return false;
}

int main(){
int T;
scanf("%d", &T);
while(T--){
for(int i = 0; i < 4; ++i) scanf("%d%d%d%d", &xx[i], &yy[i], &a[i], &b[i]);
int ans = INF;
for(int i = 0; i < 4; ++i)
for(int j = 0; j < 4; ++j)
for(int k = 0; k < 4; ++k)
for(int l = 0; l < 4; ++l){
if(i == 0) p[0] = f1(xx[0], yy[0], a[0], b[0]);
else if(i == 1) p[0] = f2(xx[0], yy[0], a[0], b[0]);
else if(i == 2) p[0] = f3(xx[0], yy[0], a[0], b[0]);
else if(i == 3) p[0] = f4(xx[0], yy[0], a[0], b[0]);

if(j == 0) p[1] = f1(xx[1], yy[1], a[1], b[1]);
else if(j == 1) p[1] = f2(xx[1], yy[1], a[1], b[1]);
else if(j == 2) p[1] = f3(xx[1], yy[1], a[1], b[1]);
else if(j == 3) p[1] = f4(xx[1], yy[1], a[1], b[1]);

if(k == 0) p[2] = f1(xx[2], yy[2], a[2], b[2]);
else if(k == 1) p[2] = f2(xx[2], yy[2], a[2], b[2]);
else if(k == 2) p[2] = f3(xx[2], yy[2], a[2], b[2]);
else if(k == 3) p[2] = f4(xx[2], yy[2], a[2], b[2]);

if(l == 0) p[3] = f1(xx[3], yy[3], a[3], b[3]);
else if(l == 1) p[3] = f2(xx[3], yy[3], a[3], b[3]);
else if(l == 2) p[3] = f3(xx[3], yy[3], a[3], b[3]);
else if(l == 3) p[3] = f4(xx[3], yy[3], a[3], b[3]);

if(judge()) ans = min(ans, i + j + k + l);
}
if(ans == INF) printf("-1\n");
else printf("%d\n", ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  codeforces 暴力