본문 바로가기
Algorithm(CodeTree, Python)/완전탐색2

[코드트리 물체 단위로 완전탐색] 겹치지 않는 선분 2 python

by kurooru 2022. 12. 17.
# n 입력
n = int(input())
# line_list
line_list = list()
# line_list 입력
for _ in range(n):
    line_list.append(tuple(map(int, input().split())))

# 함수들
# is_duplicated(k)
def is_duplicated(k):
    
    # 현재 선분의 x1, x2 언팩킹
    x1, x2 = line_list[k]

    # 비교할 선분들 탐색 시작
    for i in range(n):
        
        # 현재 선분
        if i == k:
            # skip
            continue
        
        # 이외의 선분(비교 선분) 언팩킹
        other_x1, other_x2 = line_list[i]

        # 현재 선분보다 비교 선분이 뒤에서 출발하는 선분일 경우
        if other_x1 < x1:
            # other_x2가 x2보다 앞에 있으면
            if other_x2 > x2:
                # 겹침
                return True
        
        # 현재 선분보다 비교 선분이 앞에서 출발하는 경우
        else:
            # other_x2가 x2보다 뒤에 있으면
            if other_x2 < x2:
                # 겹침
                return True
    
    # 모두 비교했으면 실패
    return False

# 설계
# cnt
cnt = 0

# 완전 탐색 시작
for i in range(n):
    # 다른 선분과 겹치지 않는 선분이면
    if not is_duplicated(i):
        # cnt에 추가
        cnt += 1

# 출력
print(cnt)