알고리즘
백준 1485번 정사각형 C++
영춘권의달인
2022. 12. 12. 16:33
https://www.acmicpc.net/problem/1485
1485번: 정사각형
첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스는 네 줄로 이루어져 있으며, 점의 좌표가 한 줄에 하나씩 주어진다. 점의 좌표는 -100,000보다 크거나 같고, 100,000보다 작거나 같
www.acmicpc.net
네 변의 길이가 같고 두 대각선의 길이가 같으면 정사각형이다. 정렬한 뒤 구하면 된다.
#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>
using namespace std;
using int64 = long long;
double GetDist(pair<int64, int64>& loc1, pair<int64, int64>& loc2) {
return sqrt(pow(loc2.first - loc1.first, 2) + pow(loc2.second - loc1.second, 2));
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int t;
cin >> t;
while (t--) {
vector<pair<int64, int64>> v;
for (int i = 0; i < 4; i++) {
int x, y;
cin >> x >> y;
v.push_back({ x,y });
}
sort(v.begin(), v.end());
double height1 = GetDist(v[0], v[1]);
double height2 = GetDist(v[2], v[3]);
double width1 = GetDist(v[0], v[2]);
double width2 = GetDist(v[1], v[3]);
double diagonal1 = GetDist(v[0], v[3]);
double diagonal2 = GetDist(v[1], v[2]);
set<double> s1;
set<double> s2;
s1.insert(height1);
s1.insert(height2);
s1.insert(width1);
s1.insert(width2);
s2.insert(diagonal1);
s2.insert(diagonal2);
int ans = 0;
if (s1.size() == 1 && s2.size() == 1) ans = 1;
cout << ans << "\n";
}
}