edu-pi / SOMA

0 stars 0 forks source link

[알고리즘] 이번주 알고리즘 문제 풀기 #13

Closed SongGwanSeok closed 3 months ago

SongGwanSeok commented 3 months ago

📝 Description

무엇을?

왜?

❗️Todo

ETC

기타사항

SongGwanSeok commented 3 months ago

https://leetcode.com/problems/valid-square/description/

SongGwanSeok commented 3 months ago

정사각형에서 한점을 잡았을 때 나머지 세점까지의 길이는 1, 1, √2가 나오는 성질 이용

class Solution {
public:

    double calDist(vector<int> now, vector<int> next){
        return pow(now[0] - next[0], 2) + pow(now[1] - next[1], 2);
    }

    bool validSquare(vector<int>& p1, vector<int>& p2, vector<int>& p3, vector<int>& p4) {
        vector<vector<int>> point = {p1, p2, p3, p4};

    for (const vector<int>& target: point) {
        unordered_set<double> dists;
        for (const vector<int>& nextPoint: point) {
            if(target == nextPoint) continue;
            dists.insert(calDist(target, nextPoint));
        }
        if(dists.size() != 2) return false;

        vector<double> dists_vec;
        for(double d : dists) {
            dists_vec.push_back(d);
        }
        if(dists_vec[0] * 2 != dists_vec[1]
        && dists_vec[1] * 2 != dists_vec[0])
        return false;
    }
    return true;
    }
};