본문 바로가기

코딩 문제/백준

[백준_3190번] 뱀 (python)


https://www.acmicpc.net/problem/3190

 

3190번: 뱀

 'Dummy' 라는 도스게임이 있다. 이 게임에는 뱀이 나와서 기어다니는데, 사과를 먹으면 뱀 길이가 늘어난다. 뱀이 이리저리 기어다니다가 벽 또는 자기자신의 몸과 부딪히면 게임이 끝난다. 게임

www.acmicpc.net

내 풀이

import sys
from collections import deque
# input = sys.stdin.readline
N = int(input()) # 맵크기 n*n
K = int(input())
apple = [tuple(map(int, input().split())) for _ in range(K)]
L = int(input())
moves = {}
for i in range(L):
    X, C = input().split()
    moves[int(X)] = C

dir_ = [(0, 1), (1, 0), (0, -1), (-1, 0)] ## 우, 하, 좌, 상
MAP = [[0] * N for _ in range(N)]
for r, c in apple:
    MAP[r-1][c-1] = 'a'

def sol():
    global MAP
    d = 0
    time = 0
    body = deque([(0, 0)])
    nx, ny = 0, 0
    while True:
        nx = nx + dir_[d][0]
        ny = ny + dir_[d][1]
        time += 1
        if nx < 0 or ny < 0 or nx >= N or ny >= N or (nx, ny) in body:
            return time
        if MAP[nx][ny] == 'a':
            MAP[nx][ny] = 0
        else:
            body.popleft()
        body.append((nx, ny))

        if time in moves.keys():
            if moves[time] == 'L':
                d = (d - 1)
            else:
                d = (d + 1)
            if d >= 4:
                d -= 4
            if d < 0 :
                d += 4

print(sol())

Feedback

  • 없뜸 ! 10분 컷