CodeForces 474C - Captain Marmot

本文探讨了一道算法题目,该题目要求通过旋转四个点来判断是否能够构成一个正方形,并给出了具体的实现代码。通过尝试所有可能的旋转组合,算法最终确定了最少的操作次数。


题意:每组共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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值