SRM 668 DIV 2 IsItASquare 600-point

本文介绍了一种算法,用于判断平面上四个不同点是否能构成一个正方形。通过计算点之间的距离及其对角线中点是否重合来确定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

It’s a bird! It’s a plane! No, it’s a square in a plane! Wait, is it really a square? There are four distinct points in the plane. You are given their coordinates in the vector s x and y: for each i between 0 and 3, inclusive, there is a point at (x[i], y[i]). Return “It’s a square” (quotes for clarity) if the four points are the vertices of a square. Otherwise, return “Not a square”.
Definition

Class:
IsItASquare
Method:
isSquare
Parameters:
vector , vector
Returns:
string
Method signature:
string isSquare(vector x, vector y)
(be sure your method is public)
Limits

Time limit (s):
2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

x will contain 4 elements.

y will contain 4 elements.

Each element of x will be between 0 and 10,000, inclusive.

Each element of y will be between 0 and 10,000, inclusive.

The four points described by x and y will be distinct.
Examples
0)

{0, 0, 2, 2}
{0, 2, 0, 2}
Returns: “It’s a square”

1)

{0, 1, 5, 6}
{1, 6, 0, 5}
Returns: “It’s a square”
Note that the sides of the square do not have to be parallel to the coordinate axes. Also note that the order in which the points are given does not have to be the same as the order in which you would encounter them when following the boundary of the square.
2)

{0, 0, 7, 7}
{0, 3, 0, 3}
Returns: “Not a square”

3)

{0, 5000, 5000, 10000}
{5000, 0, 10000, 5000}
Returns: “It’s a square”

4)

{1, 2, 3, 4}
{4, 3, 2, 1}
Returns: “Not a square”

5)

{0, 5, 3, 8}
{0, 0, 4, 4}
Returns: “Not a square”

This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.

My Solution

#include <iostream>
#include <vector>
#include <string>
using namespace std;

class IsItASquare
{
    public:
        string isSquare(vector <int> x, vector <int> y);

    private:
        //距离平方
        double distance(int x1, int y1, int x2, int y2);

        //对角线的中点是否重合
        bool isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4);
};

string IsItASquare::isSquare(vector <int> x, vector <int> y)
{
    string ans;

    if (x.size() != 4 || y.size() != 4)
    {
        ans = "Not a square";
        return ans;
    }

    for (int i = 0; i < 4; i++)
    {
        for (int j = i + 1; j < 4; j++)
            if (x[i] == x[j] && y[i] == y[j]) 
            {
                ans = "Not a square";
                return ans;
            }
    }
    if (distance(x[0], y[0], x[1], y[1]) == distance(x[2], y[2], x[3], y[3])  && isMidSame(x[0], y[0], x[1], y[1], x[2], y[2], x[3], y[3]) && distance(x[0], y[0], x[2], y[2]) == distance(x[2], y[2], x[1], y[1]) )
        ans = "It's a square";
    if (distance(x[0], y[0], x[2], y[2]) == distance(x[1], y[1], x[3], y[3])  && isMidSame(x[0], y[0], x[2], y[2], x[1], y[1], x[3], y[3]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[2], y[2]) )
        ans = "It's a square";
    if (distance(x[0], y[0], x[3], y[3]) == distance(x[1], y[1], x[2], y[2])  && isMidSame(x[0], y[0], x[3], y[3], x[1], y[1], x[2], y[2]) && distance(x[0], y[0], x[1], y[1]) == distance(x[1], y[1], x[3], y[3]) )
        ans = "It's a square";

    ans = "Not a square";

    return ans;
}

double IsItASquare::distance(int x1, int y1, int x2, int y2)
{
    return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}

bool IsItASquare::isMidSame(int x1, int y1, int x2, int y2, int x3, int y3, int x4, int y4)
{
    if (x1 + x2 == x3 + x4 && y1 + y2 == y3 + y4)
        return true;
    else
        return false;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值