https://www.acmicpc.net/problem/3190
3190번: 뱀’Dummy’ 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임www.acmicpc.net
그냥구현이다
하나 아쉬운점은 따로 뱀을 사전으로 나타내지 않고
그냥 arr에 나타내면 더 빠르다.
이게 더 간단한데 내가 왜 사전을 썼는지 몰겠다.
import sysinput=sys.stdin.readlinefrom collections import dequen=int(input())
arr=[[0 for _ in range(n)]for _ in range(n)]for _ in range(int(input())):
a,b=map(int,input().split()) arr[a-1][b-1]=1info=dict() #방향변환정보
l=int(input())for _ in range(l): a,b=input().split() info[int(a)]=b
r,c=(0,0) #가로세로좌표dd=[[0,1],[1,0],[0,-1],[-1,0]] #보고있는방향d=0 #방향 (첨에오른쪽)t=0 #시간
snake=deque([(0,0)]) #뱀본체ss=dict() #뱀위치장용ss[(0,0)]=1while True: t+=1
a,b=dd[d] r,c=r+a,c+b
if 0<=r<n and 0<=c<n and (r,c) not in ss: #벽이나 자기자신과 부딪치지 않았다면
snake.append((r,c)) ss[(r,c)]=1
if not arr[r][c]: # 이동한 칸에 사과가 없다면 tmp=snake.popleft() #꼬리자르기
del(ss[tmp]) #꼬리자르기 else: arr[r][c]=0
else: #종료 break if t in info: #방향정보변환이 있다면
if info[t]=='L': d-=1 if d<0:
d=3 else: d+=1
if d>3: d=0 #여기까지print(t)