링크 : https://www.acmicpc.net/problem/17281
17281번: ⚾
⚾는 9명으로 이루어진 두 팀이 공격과 수비를 번갈아 하는 게임이다. 하나의 이닝은 공격과 수비로 이루어져 있고, 총 N이닝 동안 게임을 진행해야 한다. 한 이닝에 3아웃이 발생하면 이닝이 종
www.acmicpc.net
- 문제
- 소요시간: 1시간 30분
- 설계하기(접근방법)
1. 입력받기
이닝별 타자의 결과를 입력받는다
2. 구현하기
타순은 4번째 타자를 1번으로 고정을 하고
permutations로
base를 구현하는 것이 까다로웠다
3. 출력하기
- 코드(출력)
import sys
from itertools import permutations
input = sys.stdin.readline
def find_score(order, n, result):
score = 0
p = 0 # 타자 순서
for inning in range(n):
out_count = 0
base = [0, 0, 0]
while out_count < 3:
player_now = order[p] - 1
record = result[inning][player_now]
if record == 0:
out_count += 1
elif record == 4:
score += 1 + sum(base)
base = [0, 0, 0]
else:
for i in range(2, -1, -1):
if base[i] == 1:
if i + record >= 3:
score += 1
else:
base[i + record] = 1
base[i] = 0
base[record - 1] = 1
p = (p + 1) % 9
return score
# 이닝
n = int(input())
result = [list(map(int, input().split())) for _ in range(n)]
max_score = 0
fixed_order = [1] * 9
for i, perm in enumerate(permutations(range(2, 10), 8), start=1):
order = list(perm[:3]) + [1] + list(perm[3:])
max_score = max(max_score, find_score(order, n, result))
print(max_score)
- 얻어갈 부분
1. 시간초과가 난 코드이다. 개념만 익혀놓자
if inning[p] == 0:
out += 1
elif inning[p] == 1:
score += b3
b1, b2, b3 = 1, b1, b2
elif inning[p] == 2:
score += b2 + b3
b1, b2, b3 = 0, 1, b1
elif inning[p] == 3:
score += b1 + b2 + b3
b1, b2, b3 = 0, 0, 1
else:
score += b1 + b2 + b3 + 1
b1, b2, b3 = 0, 0, 0
이와 같은 형태로 구현하는 것도 직관적이었을 것 같다
'알고리즘(백준) > 구현' 카테고리의 다른 글
[알고리즘/구현] 13335번 : 트럭 - python (0) | 2024.03.01 |
---|---|
[알고리즘/구현] 1713번 : 후보 추천하기 - python (0) | 2024.03.01 |
[알고리즘/구현] 3190번 : 뱀 - python (0) | 2024.02.27 |
[알고리즘/구현] 20056번 : 마법사 상어와 파이어볼 - python (0) | 2024.02.26 |
[알고리즘/구현] 17144번 : 미세먼지 안녕! - python (0) | 2024.02.23 |