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

[코드트리 기준을 새로 설정하여 완전탐색] 독서실의 거리두기 4 Python

by kurooru 2023. 1. 5.
# n 입력
n = int(input())
# study_cafe 입력
study_cafe = input()

# 함수들
# get_min_dist(idx_1, idx_2)
def get_min_dist(idx_1, idx_2):

    # seats_filled_with_user
    seats_filled_with_user = [idx_1, idx_2]

    # 독서실을 돌며
    for i in range(n):
        # 자리에 사람이 있으면
        if study_cafe[i] == '1':
            # 위치를 기록
            seats_filled_with_user.append(i)
    
    # seats_filled_with_user 정렬
    seats_filled_with_user.sort()

    # num_of_filled_seats
    num_of_filled_seats = len(seats_filled_with_user)

    # min_dist
    min_dist = 101

    # seats_filled_with_user을 돌며
    for i in range(num_of_filled_seats - 1):
        # curr_dist
        curr_dist = seats_filled_with_user[i+1] - seats_filled_with_user[i]

        # min_dist update
        min_dist = min(min_dist, curr_dist)
    
    # 반환
    return min_dist

# 설계
# empty_seat
empty_seat = []

# 빈자리의 좌표 구하기
for i in range(n):
    if study_cafe[i] == '0':
        empty_seat.append(i)

# num_of_empty_seat
num_of_empty_seat = len(empty_seat)

# max_dist
max_dist = 0

# 완전 탐색 시작 -> 빈 자리 두개를 채워가며 탐색
for i in range(num_of_empty_seat - 1):
    for j in range(i+1, num_of_empty_seat):
        # max_dist update
        max_dist = max(get_min_dist(empty_seat[i], empty_seat[j]), max_dist)

# 출력
print(max_dist)