[BOJ 1485, Python] Square
Python Solution to BOJ Problem 1485, "Square"
Problem Link
Categories
Geometry, Sorting
Description
You can determine whether a shape is a square by following these steps.
- Check the lengths of all 6 edges connecting the 4 points
- Sort them
- Check if the first 4 lengths are equal
- Check if the last 2 lengths are suitable as the diagonals of a square
- Only if all conditions are satisfied, treat it as a valid square
Solution Code
# 정사각형
import sys
from math import sqrt, isclose
from itertools import combinations
input = sys.stdin.readline
def distance(coord_1: list, coord_2: list) -> int:
return sqrt(pow(coord_1[0] - coord_2[0], 2) + pow(coord_1[1] - coord_2[1], 2))
testcase = int(input())
output = []
for _ in range(testcase):
is_square = True
coord_info = []
for _ in range(4):
coord_info.append(list(map(int, input().split())))
edge_info = []
for edge_distance in combinations(coord_info, 2):
edge_info.append(distance(edge_distance[0], edge_distance[1]))
edge_info.sort()
for idx in range(1, 4):
if not isclose(edge_info[0], edge_info[idx]):
is_square = False
break
if is_square:
for idx in range(4, 6):
if not isclose(pow(edge_info[0], 2) * 2, pow(edge_info[idx], 2)):
is_square = False
break
if is_square:
output.append(1)
else:
output.append(0)
for result in output:
print(result)
댓글 작성
게시글에 대한 의견을 남겨 주세요.