알고리즘 스터디
[백준] 17281번 : 야구 - Python(파이썬)
히치키치
2021. 5. 25. 16:47
문제
https://www.acmicpc.net/problem/17281
17281번: ⚾
⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝 동안 게임을 진행해야 한다. 한 이닝에 3아웃이 발생하면 이닝이 종
www.acmicpc.net
풀이
- 야구 구현 :
- one iterator : out count, base1, base2, base3 = 0
- 3 아웃 전까지 게임 진행
- 홈으로 들어온 사람 수 = result에 따른 총점 / base 갱신
- 다음 타자로 순번 넘겨주기
- 3 아웃 후 최고 점수 갱신
- 4번 타자 고정, 순열 조합
- 빡빡한 시간 제한 : 최소한의 함수, 최대 단일 변수 이용
참고
https://rhdtka21.tistory.com/89
[파이썬 | BOJ | 17281] ⚾
https://www.acmicpc.net/problem/17281 17281번: ⚾ ⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝동안 게임을 진행해야 한다.
rhdtka21.tistory.com
코드
#https://www.acmicpc.net/problem/17281
import sys
from itertools import permutations
read = sys.stdin.readline
N = int(read())
results = [list(map(int, read().split())) for _ in range(N) ]
# (index+1)타순에 value번 선수가 친다.
# 4번타자는 0번 선수로 고정
# 8!개의 경우의 수
maxScore = -1
for order in permutations((range(1, 9)), 8):
order = list(order[:3]) + [0] + list(order[3:])
hitter = 0
score = 0
for i in range(N):
# 한 이닝에서 유지되는것들
outcount = 0
first, second, third = 0, 0, 0
while outcount < 3: #3아웃이 될때까지 반복
# 같은 타석에서 유지되는 것들
result = results[i][order[hitter]]
if result == 0: #아웃
outcount +=1
elif result == 1: #안타
score += third
first, second, third = 1, first, second
elif result == 2: #2루타
score += (second + third)
first, second, third = 0, 1, first
elif result == 3: #3루타
score += (first + second + third)
first, second, third = 0, 0, 1
elif result == 4: #홈런
score += (first + second + third + 1)
first, second, third = 0, 0, 0
hitter = (hitter + 1) % 9 #다음타자
if maxScore < score:
maxScore = score
print(maxScore)