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;
}
每个点有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;
}
相关文章推荐
- 用fail2ban阻止SSH和VSFTP暴力破解密码
- 2015BJOI day1第三题 糖果candy
- Codeforces Round #197 (Div. 2)
- Codeforces Round #198 (Div. 1)
- Codeforces 405E Codeforces Round #238 (Div. 2)E
- Codeforces 407C Codeforces Round #239 (Div. 1)C
- 4495: Least Prime factor 找到最小质因子P的第N小正整数
- CodeForces 449A - Jzzhu and Chocolate
- CodeForces 449 B. Jzzhu and Cities
- codeforces 618C. Constellation
- Codeforces Round #349 (Div. 2) - C
- Codeforces Round #361 (Div. 2)
- 7.13Codeforces Round #360 (Div. 2)
- Codeforces Round #363 (Div. 2)
- Codeforces Round #265 (Div. 2)
- Codeforces #310 div2 C. Case of Matryoshkas
- 状态压缩DP codeforces 244 Problem C. The Brand New Function 和 codeforces 165 E. Compatible Numbers
- codeforces 16 Problem E fish
- Codeforces Round332 部分题解
- HDU 2082