SWEA 5644 무선 충전(with Python)

daeungdaeung·2021년 10월 5일
0
T = int(input())

dir_dict = {
    0: [0, 0],
    1: [0, -1],
    2: [1, 0],
    3: [0, 1],
    4: [-1, 0],
}

for tc in range(1, 1+T):
    # M: 이동 시간, A: 무선 충전기 개수
    M, A = map(int, input().split())

    a_directions = list(map(int, input().split()))
    b_directions = list(map(int, input().split()))

    battery_chargers = []
    for _ in range(A):
        # x, y, coverage, power
        battery_chargers.append(list(map(int, input().split())))

    a_position = [1, 1]
    b_position = [10, 10]

    # 총 충전량 -> 출력해야 하는 값
    answer = 0

    # 초기 위치에서 충전량 계산하기
    a_max = 0
    b_max = 0
    for charger in battery_chargers:
        if abs(a_position[0] - charger[0]) + abs(a_position[1] - charger[1]) <= charger[2]:
            if a_max < charger[3]:
                a_max = charger[3]
        if abs(b_position[0] - charger[0]) + abs(b_position[1] - charger[1]) <= charger[2]:
            if b_max < charger[3]:
                b_max = charger[3]

    answer += a_max + b_max

    for turn in range(M):
        a_dir = a_directions[turn]
        a_position[0] += dir_dict[a_dir][0]
        a_position[1] += dir_dict[a_dir][1]

        b_dir = b_directions[turn]
        b_position[0] += dir_dict[b_dir][0]
        b_position[1] += dir_dict[b_dir][1]

        a_can_charging = []
        b_can_charging = []

        for charger in battery_chargers:
            if abs(a_position[0] - charger[0]) + abs(a_position[1] - charger[1]) <= charger[2]:
                a_can_charging.append(charger)
            if abs(b_position[0] - charger[0]) + abs(b_position[1] - charger[1]) <= charger[2]:
                b_can_charging.append(charger)

        # brute-forth로 최대값을 찾자
        max_sum = 0
        len_a_can_charging = len(a_can_charging)
        len_b_can_charging = len(b_can_charging)
        # a만 충전 불가능한 경우
        if not len_a_can_charging and len_b_can_charging:
            for b_candidate in b_can_charging:
                if max_sum < b_candidate[3]:
                    max_sum = b_candidate[3]
        # b만 충전 불가능한 경우
        elif len_a_can_charging and not len_b_can_charging:
            for a_candidate in a_can_charging:
                if max_sum < a_candidate[3]:
                    max_sum = a_candidate[3]
        # 둘다 충전 가능한 경우
        elif len_a_can_charging and len_b_can_charging:
            for a_candidate in a_can_charging:
                for b_candidate in b_can_charging:
                    if a_candidate == b_candidate:
                        if max_sum < a_candidate[3]:
                            max_sum = a_candidate[3]
                    else:
                        if max_sum < a_candidate[3] + b_candidate[3]:
                            max_sum = a_candidate[3] + b_candidate[3]

        answer += max_sum

    print(f'#{tc} {answer}')
profile
개발자가 되고싶읍니다...

0개의 댓글