반응형
* 베이스상태를 list의 요소로( 1루:base[0], 2루:base[1], 3루:base[2] )로 했을때는 시간초과.
* 베이스상태를 1루는 a, 2루는 b, 3루는 c 로 바꿔주면 통과
1. 베이스를 리스트로(시간초과)
from itertools import permutations
inning=[]
n=int(input())
for _ in range(n):
inning.append(list(map(int,input().split())))
permu=list(permutations([i for i in range(1,9)],8))
ans=0
#각 경우 확인
for case in permu:
nowCase=list(case)
nowCase.insert(3,0)
nowCase=tuple(nowCase)
#print(nowCase)
hitter=0
score = 0
#이닝전체 시작
for inn in range(n):
outCnt=0
base=[0,0,0]
while(outCnt!=3):
#타석
swing=inning[inn][nowCase[hitter]]
#print("타자",nowCase[hitter],"의 결과는",swing)
#결과
if swing==0:
outCnt+=1
elif swing==1:
score+=base[2]
base[2]=base[1]
base[1]=base[0]
base[0]=1
elif swing==2:
score+=base[1]+base[2]
base[2]=base[0]
base[1]=1
base[0]=0
elif swing==3:
score+=sum(base)
base[2]=1
base[1]=base[0]=0
else:
score+=sum(base)+1
base[2]=base[1]=base[0]=0
hitter = (hitter + 1) % 9
#print("현재베이스",base,"점수는",score)
#print("최종점수는",score)
if score>ans:
ans=score
print(ans)
2. 베이스를 변수로(통과)
from itertools import permutations
inning=[]
n=int(input())
for _ in range(n):
inning.append(list(map(int,input().split())))
permu=list(permutations([i for i in range(1,9)],8))
ans=0
#각 경우 확인
for case in permu:
nowCase=list(case)
nowCase.insert(3,0)
nowCase=tuple(nowCase)
#print(nowCase)
hitter=0
score = 0
#이닝전체 시작
for inn in range(n):
outCnt=0
a=b=c=0
while(outCnt!=3):
#타석
swing=inning[inn][nowCase[hitter]]
#print("타자",nowCase[hitter],"의 결과는",swing)
#결과
if swing==0:
outCnt+=1
elif swing==1:
score+=c
c=b
b=a
a=1
elif swing==2:
score+=b+c
c=a
b=1
a=0
elif swing==3:
score+=a+b+c
c=1
b=a=0
else:
score+=a+b+c+1
c=b=a=0
hitter = (hitter + 1) % 9
#print("현재베이스",base,"점수는",score)
#print("최종점수는",score)
if score>ans:
ans=score
print(ans)
반응형