시뮬레이션 유형
import sys
from collections import deque
input=sys.stdin.readline
N=int(input())
K=int(input())
dirs=[(0,1),(1,0),(0,-1),(-1,0)]
board=[[0]*N for _ in range(N)]
for _ in range(K):
r,c=map(int,input().split())
board[r-1][c-1]='*'
num=int(input())
moves=deque()
for _ in range(num):
time,move=input().split()
moves.append((int(time),move))
idx=0
board[0][0]=1
cur_x,cur_y=0,0
tail=deque()
for i in range(1,10001): # i는 time을 의미
dir_x, dir_y = dirs[idx]
tail.append((cur_x, cur_y))
nx,ny=cur_x+dir_x,cur_y+dir_y
if nx<0 or nx>=N or ny<0 or ny>=N: break # 벽에 부딪히면
if board[nx][ny]==1: break # 자기 몸에 부딪히면
if board[nx][ny]=='*': # 사과가 있다면
board[nx][ny]=1
else:
board[nx][ny]=1
board[tail[0][0]][tail[0][1]]=0
tail.popleft()
if len(moves)>0 and i==moves[0][0]: # 방향을 전환할 시간이면
if moves[0][1]=='D': idx = (idx + 1) % 4
else: idx = (idx - 1) % 4
moves.popleft()
cur_x+=dir_x
cur_y+=dir_y
print(i)
https://www.acmicpc.net/problem/3190
3190번: 뱀
'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임
www.acmicpc.net
'IT > coding study' 카테고리의 다른 글
[acmicpc] 17142. 연구소 3(python) (0) | 2022.03.16 |
---|---|
[acmicpc] 3020. 개똥벌레 (python) (0) | 2022.03.10 |
[programmers] [3차] n진수 게임 (python) (0) | 2022.01.24 |
[programmers] [3차] 압축 (python) (0) | 2022.01.18 |
[programmers] [3차] 방금그곡 (python) (0) | 2022.01.17 |