๋ญ ์ด๋ฐ ๋ฌธ์ ๊ฐ ๋ค ์๋...
โ๏ธ ํ์์ ์์๋ฅผ ์ ํด์
โ๏ธ ์ ํด์ง ๊ท์น์ ์ง์ผ์ N๋ฒ์ ์ด๋๊น์ง ๊ฒฝ๊ธฐ ์งํํด์
โ๏ธ ์ป์ ์ ์๋ ์ต๋ ์ ์๋ฅผ ๊ตฌํ๋ฉด ๋๋ค
4๋ฒ์งธ ํ์๋ ๋ฌด์กฐ๊ฑด 1๋ฒ ์ ์์ฌ์ผ ํ๋ค
์ฌ์ค 9๋ช ํ์์ ์์๋ฅผ ๋ค ์ ํ๋ฉด ๋๋ต 36๋ง์ ๋์ธ๋ฐ 1๋ช ์ ์ด๋ฏธ ์ ํด์ ธ์๊ธฐ ๋๋ฌธ์ 8๋ช ๋ง ์ ํ๋ฉด ๋ผ์ ๋๋ต 4๋ง ์ ๋๋ก ์ค์ด๋ ๋ค
์ฌ๊ท๋ฅผ ์ฌ์ฉํด์ ๋ ธ๊ฐ๋ค๋ก ๊ตฌํ๊ณ ๋น์ฐํ ์๊ฐ์ด๊ณผใ ใ
visited[0] = True
order[3] = 0
def perm(k):
if k == 3:
perm(4)
return
if k == 9:
baseball(order)
return
for i in range(9):
if visited[i] == False:
order[k] = i
visited[i] = True
perm(k+1)
order[k] = -1
visited[i] = False
perm(0)
itertools ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ permutations(์์ด)์ ์ฌ์ฉํด์ 1์ ์ ์ธํ ๋๋จธ์ง ์ซ์๋ค๋ก ์์ด์ ๋ง๋ค๊ณ 4๋ฒ์งธ ์๋ฆฌ์ 1๋ฒ์ ๋ผ์๋ฃ๋ ๋ฐฉ์์ผ๋ก ๋ง๋ค์ด์ผ ์๊ฐ์ด๊ณผ๊ฐ ๋์ง ์์
์ฒ์์ ์์์ ์ฌ๊ท๋ก ์์ด ๋ง๋๋ ๋ฐฉ์์ฒ๋ผ ํด์ ํ์์ ์์๊ฐ ๋ค ์ ํด์ง๋ฉด baseball
ํจ์๋ฅผ ํตํด์ ๊ฒ์์ ์งํํ๋ ค๊ณ ํ๋๋ฐ ๋น์ฐํ ์๊ฐ์ด๊ณผ๊ฐใ
ใ
โ๏ธ out์ด 3๋ฒ ๋ฐ์ํ๋ฉด ๋ค์ ์ด๋์ผ๋ก
โ๏ธ ๋ค์ ์ด๋์ผ๋ก ๋์ด๊ฐ๋ ํ์์ ์์๋ ๊ณ์ ์ด์ด์ ธ์ผ ํจ
์ด๋์ด N๋ฒ์ ๋๊ธธ๋๊น์ง ๋ฐ๋ณต๋ฌธ์ผ๋ก ๊ฒฝ๊ธฐ๋ฅผ ๊ณ์ํ๊ฒ ๋๋ค. ์ด๋ ๋ฐ๋ณตํ์๊ฐ ์ ํด์ ธ ์์ง ์๊ธฐ ๋๋ฌธ์ while
์ ์ฌ์ฉํ๊ฒ ๋จ. ์๋๋ ๊ฐ ์ ์๋ณ๋ก ๋ถ๊ธฐ ์ฒ๋ฆฌ.
0๏ธโฃ ์์ : out
1์ฆ๊ฐ
1๏ธโฃ ์ํ : 1์นธ์ฉ ์ด๋, 3๋ฃจ ์ ์ ์ ์ ์ถ๊ฐ, 1๋ฃจ์ ์ ์ ์ถ๊ฐ
2๏ธโฃ 2๋ฃจํ : 2์นธ์ฉ ์ด๋, 2-3๋ฃจ ์ ์ ์ ์ ์ถ๊ฐ, 2๋ฃจ์ ์ ์ ์ถ๊ฐ
3๏ธโฃ 3๋ฃจํ : 3์นธ์ฉ ์ด๋, 1-2-3๋ฃจ ์ ์ ์ ์ ์ถ๊ฐ, 3๋ฃจ์ ์ ์ ์ถ๊ฐ
4๏ธโฃ ํ๋ฐ : 4์ ์ถ๊ฐ, ๋ชจ๋ ๋ฃจ์ ์ ์ ์์
๋ถ๊ธฐ์ฒ๋ฆฌ๊ฐ ๋๋๋ฉด out
์ด 3์ ๋๋ ๊ฒฝ์ฐ inning
1์ถ๊ฐ, ๋ชจ๋ ๋ฃจ์์ ์ ์ ์ ๊ฑฐ, out
0์ผ๋ก ์ด๊ธฐํํ๋ ์ธ ๊ฐ์ง ์์
1, 2, 3๋ฃจ์ ๋ํ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ base ๋ฐฐ์ด์ ๊ฐ์ง๊ณ ๊ฐ ์ ์์ ๋ํด์ ๋ฐฐ์ด splice๋ฅผ ์ฌ์ฉํด์ ์ฝ๋๋ฅผ ์งฐ๋๋ฐ ๋๋ฌด ๋น์ฐํ์ธ ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค ใ ใ
import sys
from itertools import permutations
input = sys.stdin.readline
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
ans = 0
def baseball(order):
global ans
out = inning = 0
base = [0]*3
now = score = 0
while inning < N:
inn = arr[inning][order[now]]
if inn == 0:
out += 1
elif inn == 1:
score += base[0]
base = base[1:] + [1]
elif inn == 2:
score += sum(base[:2])
base = base[2:] + [1, 0]
elif inn == 3:
score += sum(base)
base = [1, 0, 0]
elif inn == 4:
score += sum(base) + 1
base = [0, 0, 0]
now = (now+1)%9
if out == 3:
inning += 1
base = [0, 0, 0]
out = 0
ans = max(ans, score)
for perm in permutations(range(1, 9)):
order = list(perm[:3]) + [0] + list(perm[3:])
baseball(order)
print(ans)
์ด๋ฐ ์์ผ๋ก ๊ฐ ๋ฃจ์ ์ ์๊ฐ ์๋์ง ์๋์ง ํ๋ณํ๊ณ ์ ์๊ฐ ์๋ ๊ฒฝ์ฐ์ ๋ํด์ ๊ฐ๊ฐ์ ์ธ๋ฑ์ค๋ง๋ค ์ฒ๋ฆฌ๋ฅผ ๋ค ๋ฐ๋ก ํด์ค์ผ ์๊ฐ ์ด๊ณผ๊ฐ ๋์ง ์์๋ค.
from itertools import permutations
N = int(input())
arr = [list(map(int, input().split())) for _ in range(N)]
c=[1,2,3,4,5,6,7,8]
c_m=permutations(c,8)
ans = 0
for case in c_m:
order = list(case)
order.insert(3, 0)
out = inning = player = score = 0
base = [0]*3
while inning < N:
inn = arr[inning][order[player]]
if inn == 0:
out += 1
elif inn == 1: # ์ํ
if base[2] == 1:
score += 1
base[2] = 0
if base[1] == 1:
base[2] = 1
base[1] = 0
if base[0] == 1:
base[1] = 1
base[0] = 1
elif inn == 2:
if base[2] == 1:
score += 1
base[2] = 0
if base[1] == 1:
score += 1
base[1] = 0
if base[0] == 1:
base[2] = 1
base[0] = 0
base[1] = 1
elif inn == 3:
if base[2] == 1:
score += 1
base[2] = 0
if base[1] == 1:
score += 1
base[1] = 0
if base[0] == 1:
score += 1
base[0] = 0
base[2] = 1
elif inn == 4:
if base[2] == 1:
score += 1
base[2] = 0
if base[1] == 1:
score += 1
base[1] = 0
if base[0] == 1:
score += 1
base[0] = 0
score += 1
player = (player+1)%9
if out == 3:
inning += 1
base = [0, 0, 0]
out = 0
ans = max(ans, score)
print(ans)