문제 : https://www.acmicpc.net/problem/1485
[알고리즘풀이]
"네 변의 길이가 같고, 대각선의 길이가 같다면 정사각형이다."
위 명제를 이용해 문제를 해결할 수 있다.
4개의 점에 대해서, 각 점들과의 거리를 모두 구하면 총 6개의 거리가 생긴다.
이때, 거리를 sort 하면 앞에 4개는 네 변의 길이가 되고, 뒤에 2개는 대각선의 길이가 된다. (대각선이 더 기므로)
따라서, 앞 4개의 길이가 같고 뒤 2개의 대각선의 길이가 같으지만 체크하면 된다.
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(void) {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t;
cin >> t;
while (t--) {
vector<pair<int, int>> p;
vector<int> s;
for (int i = 0; i < 4; i++) {
int a, b;
cin >> a >> b;
p.push_back(make_pair(a, b));
}
for (int i = 0; i < 4; i++) {
for (int j = i + 1; j < 4; j++) {
s.push_back((p[i].first - p[j].first) * (p[i].first - p[j].first) + (p[i].second - p[j].second) * (p[i].second - p[j].second));
}
}
sort(s.begin(), s.end());
if (s[0] == s[1] && s[1] == s[2] && s[2] == s[3] && s[4] == s[5])
cout << "1\n";
else
cout << "0\n";
}
}
'Problem Solving > BOJ' 카테고리의 다른 글
[BOJ] 2303 : 숫자 게임 - travelbeeee (0) | 2019.11.06 |
---|---|
[BOJ] 2162 : 선분 그룹 - travelbeeee (0) | 2019.11.05 |
[BOJ] 5557 : 1학년 - travelbeeee (0) | 2019.11.03 |
[BOJ] 1105 : 팔 - travelbeeee (0) | 2019.10.31 |
[BOJ] 12840 : 창용이의 시계 -travelbeeee (0) | 2019.10.31 |