본문 바로가기
프로그래밍/acmcpc

[2503] 숫자 야구

by blopz 2025. 8. 11.

모든 가능성에 대해서

해당 가능성이 정답일 경우 몇 스트라이크고 몇 볼일지 계산하고,

우리가 받은 스트라이크 값과 볼 값과 같으면 후보에 넣으면서 좁혀간다.

 

모든 경우의 수를 계산하려다가

하나라도 잘못 입력할 경우 문제가 꼬여서 이렇게 하는 게 맞는 것 같았다.

import sys
from itertools import permutations

N = int(sys.stdin.readline())
candidates = set(''.join(p) for p in permutations('123456789', 3))

def count_strike_ball(secret, guess):
    strikes = sum(s == g for s, g in zip(secret, guess))
    balls = len(set(secret) & set(guess)) - strikes
    return strikes, balls

for _ in range(N):
    question, strike, ball = sys.stdin.readline().split()
    strike, ball = int(strike), int(ball)
    
    new_candidates = set()
    for candidate in candidates:
        s, b = count_strike_ball(question, candidate)
        if s == strike and b == ball:
            new_candidates.add(candidate)
            
    candidates = new_candidates

print(len(candidates))

'프로그래밍 > acmcpc' 카테고리의 다른 글

[5545] 최고의 피자  (4) 2025.08.11
[1417] 국회의원 선거  (2) 2025.08.11
[2798] 블랙잭  (0) 2025.08.11
[1158] 요세푸스 문제  (2) 2025.08.11
[10845] 큐  (2) 2025.08.11