Doby's Lab

백준 1064번: 평행사변형 (C++) 본문

PS/BOJ

백준 1064번: 평행사변형 (C++)

도비(Doby) 2022. 11. 13. 20:31

https://www.acmicpc.net/problem/1064

 

1064번: 평행사변형

평행사변형은 평행한 두 변을 가진 사각형이다. 세 개의 서로 다른 점이 주어진다. A(xA,yA), B(xB,yB), C(xC,yC) 이때, 적절히 점 D를 찾아서 네 점으로 평행사변형을 만들면 된다. 이때, D가 여러 개 나

www.acmicpc.net


Solved By: Geometry, CCW

 

CCW를 통해 세 점들이 한 직선 위에 있는지 판별하여 평행사변형을 만들 수 있는지 없는지에 대해 1차적으로 구분합니다.

 

나머지는 세 점들로부터 두 점씩 짝지어 거리를 구하여 구할 수 있는 모든 평행사변형의 둘레를 구한 다음 최댓값과 최솟값의 차이를 구해줍니다.

#include <iostream>
#include <cmath>
#include <vector>
#define ld long double
using namespace std;

struct Point{
    ld x, y;
};

ld xa, ya, xb, yb, xc, yc;

ld ccw(Point a, Point b, Point c){
    return (b.x - a.x) * (c.y - a.y) - (c.x - a.x) * (b.y - a.y);
}

ld dist(Point a, Point b){
    ld x = a.x - b.x;
    ld y = a.y - b.y;
    return sqrtl(x * x + y * y);
}

ld getRound(ld a, ld b){
    return 2 * (a + b);
}

int main(){
    cout.precision(14);
    cout << fixed;
    
    Point a, b, c;
    cin >> a.x >> a.y >> b.x >> b.y >> c.x >> c.y;
    if(ccw(a, b, c) == 0){
        cout << -1;
    }
    else{
        ld ab = dist(a, b);
        ld bc = dist(b, c);
        ld ac = dist(a, c);
        
        vector<ld> v;
        v.push_back(getRound(ab, ac));
        v.push_back(getRound(ab, bc));
        v.push_back(getRound(ac, bc));
        
        ld maxValue = 0;
        ld minValue = 40000;
        for(auto i : v){
            if(i > maxValue) maxValue = i;
            if(i < minValue) minValue = i;
            //cout << i << '\n';
        }

        cout << maxValue - minValue;
    }
    return 0;
}

 

728x90

'PS > BOJ' 카테고리의 다른 글

백준 14401번: 악덕 나라 (C++)  (0) 2022.11.14
백준 1143번: 경찰 (C++)  (2) 2022.11.14
백준 10531번: Golf Bot (C++)  (0) 2022.11.12
백준 18134번: 치삼이의 대모험 (C++)  (0) 2022.11.06
백준 22990번: 사이클 (C++)  (0) 2022.11.06