167. 할리갈리 게임
1) 어떤 전략(알고리즘)으로 해결?
2) 코딩 설명
<내 풀이>
import sys
from collections import deque
n,m=map(int, sys.stdin.readline().split())
dcard=deque()
scard=deque()
dground=deque()
sground=deque()
for i in range(n) :
d,s=map(int, sys.stdin.readline().split())
dcard.append(d)
scard.append(s)
winner = 0
for i in range(m) :
if (i%2==0) :
dground.append(dcard.pop())
present=dground[-1]
else :
sground.append(scard.pop())
present=sground[-1]
if not scard :
winner = "do"
break
if not dcard :
winner = "su"
break
if (present==5) :
for j in sground:
dcard.appendleft(j)
for k in dground:
dcard.appendleft(k)
sground=deque()
dground=deque()
elif (dground and sground) :
if (dground[-1]+sground[-1])==5 :
for j in dground:
scard.appendleft(j)
for k in sground:
scard.appendleft(k)
sground=deque()
dground=deque()
if winner!=0:
print(winner)
else :
if len(scard) > len(dcard) :
winner = "su"
elif len(scard) < len(dcard) :
winner = "do"
else :
winner = "dosu"
print(winner)
<내 틀렸던 풀이, 문제점>
- append랑 appendleft의 차이였다,,
- 내가 설정한 로직에서는 리스트의 뒤 인덱스일 수록 우선순위가 높은 아이였기에, 종 친 사람이 카드 자기걸로 가져올 때,
- 기존에 있던 것들이 우선순위 더 높은 상황!
- 따라서 새로 더하는 것들을
앞으로 오게 더해야함
- 근데 나는!????? append로
뒤에 더함
;;
그래서 이를 appendleft로 고쳐주니 okay
import sys
from collections import deque
n,m=map(int, sys.stdin.readline().split())
dcard=deque()
scard=deque()
dground=deque()
sground=deque()
for i in range(n) :
d,s=map(int, sys.stdin.readline().split())
dcard.append(d)
scard.append(s)
winner = 0
for i in range(m) :
if (i%2==0) :
dground.append(dcard.pop())
present=dground[-1]
else :
sground.append(scard.pop())
present=sground[-1]
if not scard :
winner = "do"
break
if not dcard :
winner = "su"
break
if (present==5) :
for j in sground:
dcard.append(j)
for k in dground:
dcard.append(k)
sground=deque()
dground=deque()
elif (dground and sground) :
if (dground[-1]+sground[-1])==5 :
for j in dground:
scard.append(j)
for k in sground:
scard.append(k)
sground=deque()
dground=deque()
if winner!=0:
print(winner)
else :
if len(scard) > len(dcard) :
winner = "su"
elif len(scard) < len(dcard) :
winner = "do"
else :
winner = "dosu"
print(winner)
- 8프로 에서 틀린다!?

나는 근데 append 로 내 덱 뒤로 더해주고 있었는데,
뽑는 우선순위 높은 애일 수록 뒤고, 나중에 들어오는 애는 앞으로 넣어줘야 해,
나는 최신 애를 뒤에 두었기 떄문에,,,
<반성 점>
- 문제를 신중히 읽기,,
- 이를테면 아래에서 뽑는게 아니라 위에서부터 뽑는다던가! 그런,,,
<배운 점>
- queue는 더할 때 extendleft라는 것을 써서 옆에다가 이어붙이기 가능
- append 는 deque 안에 deque 가 들어가는 구조라 이상해진다.