백준 알고리즘 #2503

Zion·2023년 11월 21일
0

백준

목록 보기
4/4

백준 알고리즘 : 숫자 야구

🔒 문제

백준 알고리즘 #2503

🔑 풀이

idea

  1. 세 자리 숫자를 각각 100의 자리 a, 10의 자리 b, 1의 자리 c로 지정하여 for문을 적용
  2. 만약 a, b, c 중 같은 숫자가 있다면 continue를 이용해 for문으로 돌아 감
  3. hint의 개수를 맨 위에서 입력받고, 모든 hint를 통과해야만 답이 될 가능성이 있음.
  4. a, b, c가 각각 존재하는지 확인하고, 위치가 같다면 strike + 1, 위치가 다르면 ball + 1
  5. strike와 ball의 개수가 힌트에 나온 내용과 같다면, 해당 hint 하나를 통과 -> check를 하나 증가
  6. 임의의 세 자리 숫자가 hint를 통과할 때마다 check를 하나씩 올리게 되면, check가 hint의 개수와 같아야 함

code

N = int(input())
hints = []
count = 0

for _ in range(N):
	num, strike, ball = input().split(' ')
    hint.append([num, strike, ball])
    
for a in range(1, 10):
	for b in range(1, 10):
    	for c in range(1, 10):
        	
            if a == b or b == c or c == a :
            	continue
           
            hint_check = 0
            
            # hint = [123, 1, 1]
            for hint in hints:
            	pred_strike = 0
                pred_ball = 0
            
            	# a, b, c에 다 적용
            	if str(a) in str(hint[0]):
                # 위치까지 같으면 pred_strike + 1 아니면 pred_ball + 1
                    if str(a) == str(hint[0])[0] :
                    	pred_strike =+ 1
                    else :
                    	pred_ball =+ 1
                
                if str(b) in str(hint[0]):
                    if str(b) == str(hint[0])[1] :
                    	pred_strike =+ 1
                    else :
                    	pred_ball =+ 1
                
                if str(c) in str(hint[0]):
                    if str(c) == str(hint[0])[2] :
                    	pred_strike =+ 1
                    else :
                    	pred_ball =+ 1
                
                if pred_strike == strike and pred_ball == ball:
                	hint_check += 1
         	
            # 힌트의 개수 = 힌트 통과한 개수이면 모든 조건 만족
         	if len(hint) == hint_check :
            	count += 1
     
     
 print(count)

0개의 댓글