[백준] 2503 : 숫자 야구 (Python)
문제 링크 : https://www.acmicpc.net/problem/2503
풀이 코드
from itertools import permutations
nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
total_list = list(permutations(nums,3))
N = int(input())
ans_list = [[] for _ in range(N+1)]
ans_list[0] = total_list
for n in range(N):
in_num, num_st, num_b = map(int,input().split())
in_num = str(in_num)
for ans in ans_list[n]:
st = 0
b = 0
for idx in range(3):
if ans[idx] == in_num[idx]:
st += 1
elif in_num[idx] in ans:
b += 1
if num_st == st and num_b == b:
ans_list[n+1].append(ans)
print(len(ans_list[N]))
풀이 해설
입력받은 숫자와 스트라이크 볼 수로 가능한 케이스를 생성하는 방향으로 접근하자니 너무 복잡해져
가능한 케이스들을 점차 줄여나가는 방식으로 접근하기로 생각했습니다
처음에는 가능한 모든 조합을 itertools의 permutations를 통해 생성해냈고
그 다음 ans_list라는 list를 생성함으로써
각 입력에 따라 가능한 케이스들은 점차 줄여나가면서 그 다음 ans_list에 저장해 나가도록 풀었습니다
댓글남기기