2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트
www.acmicpc.net
어렸을 적에 많이 했던 숫자 야구이다.
그래도 나름 잘했던 게임이라 생각하는데 이런식으로 뒤통수맞을 줄이야.
- 대전략 :
생각하고 있을 답의 총 개수라면, 결국 경우의 수를 전부 뽑아내어 간추릴 수 밖에 없다.
첫번째 대답에서 1스트라이크 1볼이라고 했으니, 1개는 자릿수와 숫자가 맞고, 1개는 자릿수가 맞고, 1개는 틀렸을 경우의 수를 전부 뽑아낸 뒤, 뒤의 조건에 대입하여 소거시킬 수 밖에 없다.
일단, 입력 값을 받고 첫번째 숫자와 스트라이크, 볼을 불러오자.
t = int(sys.stdin.readline()) judge = [] start = [list(map(int,sys.stdin.readline().split())) for _ in range(t)]
그리고 전체 리스트를 만들어주자.
a = [1, 2, 3, 4, 5, 6, 7, 8, 9] numberlist = list(permutations(a, 3)) for i in range(len(numberlist)): temp = 0 for j in range(2, -1, -1): temp += (10**j)*numberlist[i][j] cand.append(temp) sys.stdin = open("input.txt","r")
그리고 스트라이크 판별식을 만들어주자.
스트라이크는 숫자와 자릿수가 일치하기 때문에 간단하게 구현가능하다.
def strike(start_num, start_strike, start_ball): remove_list = [] # 스트라이크 for i in range(3): # 스트라이크 갯수 if i == start_strike: # 스트라이크 판별 for j in range(len(cand)): cand_strike = 0 if start_num // 100 == cand[j] // 100: cand_strike += 1 if (start_num // 10) % 10 == (cand[j] // 10) % 10: cand_strike += 1 if start_num % 10 == cand[j] % 10: cand_strike += 1 if cand_strike != start_strike: remove_list.append(cand[j]) for k in range(0, len(remove_list)): cand.remove(remove_list[k])
볼도 그렇게 어렵진 않다.
def ball(start_num, start_strike, start_ball): remove_list = [] # 볼 for i in range(4): if i == start_ball: for j in range(len(cand)): cand_ball = 0 if start_num // 100 == (cand[j] // 10) % 10 or start_num // 100 == cand[j] % 10 and start_num // 100 != cand[j] // 100: cand_ball += 1 if (start_num // 10) % 10 == cand[j] // 100 or (start_num // 10) % 10 == cand[j] % 10 and (start_num // 10) % 10 != (cand[j] // 10) % 10: cand_ball += 1 if start_num % 10 == cand[j] // 100 or start_num % 10 == (cand[j] // 10) % 10 and start_num % 10 != cand[j] % 10: cand_ball += 1 if cand_ball != start_ball: remove_list.append(cand[j]) for k in range(0, len(remove_list)): cand.remove(remove_list[k])
전체 코드는 다음과 같다.
import sys from itertools import permutations cand = [] a = [1, 2, 3, 4, 5, 6, 7, 8, 9] numberlist = list(permutations(a, 3)) for i in range(len(numberlist)): temp = 0 for j in range(2, -1, -1): temp += (10**j)*numberlist[i][j] cand.append(temp) sys.stdin = open("input.txt","r") # a = int(sys.stdin.readline()) # a,b = map(int,input().split()) # a = [int(x) for x in input().strip().split()] #judge = [list(map(int,sys.stdin.readline().split())) for _ in range(t)] #read = sys.stdin.readline t = int(sys.stdin.readline()) judge = [] start = [list(map(int,sys.stdin.readline().split())) for _ in range(t)] # 전체 리스트 생성 # for i in range(1,10): # for j in range(1,10): # for k in range(0,10): # cand.append(i*100 + j*10 + k) def judging(start): global t start_strike = 0 start_ball = 0 for i in range(t): start_num = start[i][0] start_strike = start[i][1] start_ball = start[i][2] # print("i:",i,"len(cand):", len(cand)) strike(start_num, start_strike, start_ball) ball(start_num, start_strike, start_ball) print(len(cand)) def strike(start_num, start_strike, start_ball): remove_list = [] # 스트라이크 for i in range(3): # 스트라이크 갯수 if i == start_strike: # 스트라이크 판별 for j in range(len(cand)): cand_strike = 0 if start_num // 100 == cand[j] // 100: cand_strike += 1 if (start_num // 10) % 10 == (cand[j] // 10) % 10: cand_strike += 1 if start_num % 10 == cand[j] % 10: cand_strike += 1 if cand_strike != start_strike: remove_list.append(cand[j]) for k in range(0, len(remove_list)): cand.remove(remove_list[k]) def ball(start_num, start_strike, start_ball): remove_list = [] # 볼 for i in range(4): if i == start_ball: for j in range(len(cand)): cand_ball = 0 if start_num // 100 == (cand[j] // 10) % 10 or start_num // 100 == cand[j] % 10 and start_num // 100 != cand[j] // 100: cand_ball += 1 if (start_num // 10) % 10 == cand[j] // 100 or (start_num // 10) % 10 == cand[j] % 10 and (start_num // 10) % 10 != (cand[j] // 10) % 10: cand_ball += 1 if start_num % 10 == cand[j] // 100 or start_num % 10 == (cand[j] // 10) % 10 and start_num % 10 != cand[j] % 10: cand_ball += 1 if cand_ball != start_ball: remove_list.append(cand[j]) for k in range(0, len(remove_list)): cand.remove(remove_list[k]) judging(start)
코드 로직 자체는 어렵지 않았지만, 여러 자잘한 실수가 모여서 완성하는데 시간이 많이 걸렸다.
힘들다 자야지
'프로그래밍 공부 > 백뚠' 카테고리의 다른 글
아~ 와이파이 잘되는 집을 찾으시는구나? (백준 2110 : 공유기 설치) (0) | 2020.12.18 |
---|---|
나무를 자르다니 나무해 (백준 2805:나무자르기) (0) | 2020.12.18 |
아래로부터의 혁명 (백준 9095 : 1, 2, 3 더하기) (0) | 2020.12.17 |
여인천하 (백준 9669번 : N-Queen) (1) | 2020.12.16 |
이 세상의 모든 세일즈맨을 위하여 (백준 10971번 : 외판원 순회2) (0) | 2020.12.15 |
댓글