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

[코드트리 자리 마다 숫자를 정하는 완전탐색] 두 가지로 열리는 자물쇠 python

by kurooru 2022. 12. 12.

원형에서 조건 따질때,
절댓값으로 해결하면 편하다는것을 기억하도록 하자는 교훈,,,

# n 입력
n = int(input())
# comb_1 입력
comb_1 = list(map(int, input().split()))
# comb_2 입력
comb_2 = list(map(int, input().split()))

# 함수들
# open_with_comb_1(a, b, c)
def open_with_comb_1(a, b, c):
    # a, b, c와 comb_1[0], comb_1[1], comb_1[2] 각각의 간격이 모두 2 이내이면 통과
    return (abs(comb_1[0] - a) <= 2 or abs(comb_1[0] - a) >= n-2) and \
    (abs(comb_1[1] - b) <= 2 or abs(comb_1[1] - b) >= n-2) and \
    (abs(comb_1[2] - c) <= 2 or abs(comb_1[2] - c) >= n-2)

# open_with_comb_2(a, b, c)
def open_with_comb_2(a, b, c):
    # a, b, c와 comb_2[0], comb_2[1], comb_2[2] 각각의 간격이 모두 2 이내이면 통과
    return (abs(comb_2[0] - a) <= 2 or abs(comb_2[0] - a) >= n-2) and \
    (abs(comb_2[1] - b) <= 2 or abs(comb_2[1] - b) >= n-2) and \
    (abs(comb_2[2] - c) <= 2 or abs(comb_2[2] - c) >= n-2)

# 설계
# cnt
cnt = 0

# 완전 탐색 시작
for i in range(1, n+1):
    for j in range(1, n+1):
        for k in range(1, n+1):
            # 첫 번째 조합 또는 두 번째 조합으로 열리면,
            if open_with_comb_1(i,j,k) or open_with_comb_2(i,j,k):
                # cnt에 추가
                cnt += 1

# 출력
print(cnt)