동캄의 코딩도장

백준 11758 [CCW] 파이썬 본문

코테/BOJ

백준 11758 [CCW] 파이썬

동 캄 2022. 5. 20. 21:15

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

 

11758번: CCW

첫째 줄에 P1의 (x1, y1), 둘째 줄에 P2의 (x2, y2), 셋째 줄에 P3의 (x3, y3)가 주어진다. (-10,000 ≤ x1, y1, x2, y2, x3, y3 ≤ 10,000) 모든 좌표는 정수이다. P1, P2, P3의 좌표는 서로 다르다.

www.acmicpc.net

# 백준 11758 CCW
import math

P1 = list(map(int, input().split()))
P2 = list(map(int, input().split()))
P3 = list(map(int, input().split()))

if P2[0] != P1[0]:
    a = (P2[1]-P1[1])/(P2[0]-P1[0])
    b = -a*P1[0]+P1[1]
    if P1[0] > P2[0]:
        if round(a*P3[0]+b) == P3[1]:
            print(0)
        elif a*P3[0]+b > P3[1]:
            print(1)
        else:
            print(-1)
    else:
        if round(a*P3[0]+b) == P3[1]:
            print(0)
        elif a*P3[0]+b > P3[1]:
            print(-1)
        else:
            print(1)

else:
    if P1[1] < P2[1]:
        if P1[0] == P3[0]:
            print(0)
        elif P3[0] > P1[0]:
            print(-1)
        else:
            print(1)
    else:
        if P1[0] == P3[0]:
            print(0)
        elif P3[0] > P1[0]:
            print(1)
        else:
            print(-1)

주어진 조건에 따라 구현하였다. 다만, 이 문제를 풀 때 유의해야할점은 일차함수를 구현하고 나서, 값을 대입할때 반올림을 해야한다는 것이다. (나눌때 미세한 오차 발생)

 

# 외적 활용
a,b=map(int,input().split())
c,d=map(int,input().split())
e,f=map(int,input().split())
x=(c-a)*(f-b)-(e-a)*(d-b)
print(x and x//abs(x))

외적을 이용하여 문제를 해결할 수 도 있다. P2P1 X P3P1 일때, 삼각형의 넓이를 S라고하면,

S>0 반시계

S=0 일직선

S<0 시계

로 판단할 수 있다.

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

백준 2636 [치즈] 파이썬  (0) 2022.05.24
백준 2470 [두 용액] 파이썬  (0) 2022.05.24
백준 2589 [보물섬] 파이썬  (0) 2022.05.20
백준 14981 [톱니바퀴] 파이썬  (0) 2022.05.19
백준 2225 [합분해] 파이썬  (0) 2022.05.19