Search

PGS 양궁대회

URL
태그
구현
브루트포스 알고리즘

문제 설명

라이언이 어피치를 가장 큰 점수 차이로 이기기 위해 n발의 화살을 어떻게 쏘면 되는지 구하는 문제
kk(1~10사이의 자연수)점을 어피치가 aa발을 맞혔고 라이언이 bb발을 맞혔을 경우 더 많은 화살을 kk점에 맞힌 선수가 kk점을 가져가는 방식
단, a = b일 경우에는 어피치가 k점을 가져가고, a = b = 0일 경우에는 라이언과 어피치 모두 점수를 획득하지 않는다.
라이언이 가장 큰 점수 차이로 우승할 수 있는 방법이 여러 가지 일 경우, 가장 낮은 점수를 더 많이 맞힌 경우를 출력
라이언이 우승할 방법이 없는 경우 [-1] 을 출력

예제 입력/출력

n
info
result
5
[2,1,1,1,0,0,0,0,0,0,0]
[0,2,2,0,1,0,0,0,0,0,0]
1
[1,0,0,0,0,0,0,0,0,0,0]
[-1]
9
[0,0,1,2,0,1,1,1,1,1,1]
[1,1,2,0,1,2,2,0,0,0,0]
10
[0,0,0,0,0,0,0,0,3,4,3]
[1,1,1,1,1,1,1,1,0,0,2]

제약 조건

1n101 ≤ n ≤ 10
info=11|info| = 11
라이언과 어피치 모두 n발의 화살을 남김없이 쏴야한다.

문제 풀이

브루트 포스 - O(11Hn)O(_{11}H_{n})

풀이 코드

from itertools import combinations_with_replacement def compare_score(apeach, lion): lion_point = 0 apeach_point = 0 for i in range(11): if lion[i] == 0 and apeach[i] == 0: continue elif lion[i] <= apeach[i]: apeach_point += 10 - i else: lion_point += 10 - i return lion_point - apeach_point def solution(n, info): best_point = 0 best_result = [0] * 11 for comb in combinations_with_replacement(range(11), n): cur_result = [0] * 11 for num in comb: cur_result[num] += 1 cur_point = compare_score(info, cur_result) if (cur_point > best_point) or (cur_point == best_point and cur_result[::-1] > best_result[::-1]): best_point = cur_point best_result = cur_result return [-1] if best_point == 0 else best_result
Python
복사