Today
Total
07-05 02:35
관리 메뉴

T-coding

백준 3190 뱀 / Python 본문

Baekjoon

백준 3190 뱀 / Python

Tcoding 2023. 3. 21. 13:46
728x90
728x90

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

 

3190번: 뱀

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

www.acmicpc.net

 

 

코드

더보기
from collections import deque

n = int(input())
k = int(input())
apple = []
rot = {}
dy = [0, 1, 0, -1]
dx = [1, 0, -1, 0]
for _ in range(k):
    a, b = map(int, input().split())
    apple.append((a, b))
l = int(input())
for _ in range(l):
    a, b = map(str, input().split())
    rot[int(a)] = b

ans = 0
q = deque()
q.append((1, 1))
y, x = 1, 1
dirs = 0
while 1:
    ans += 1
    ny = y + dy[dirs]
    nx = x + dx[dirs]
    if 1 <= ny <= n and 1 <= nx <= n and q.count((ny, nx)) == 0:
        if (ny, nx) not in apple:
            q.pop()
        else:
            apple.remove((ny, nx))
        q.appendleft((ny, nx))

        y = ny
        x = nx
    else:
        break

    if ans in rot.keys():
        if rot[ans] == 'L':
            dirs = (dirs + 3) % 4
        else:
            dirs = (dirs + 1) % 4

print(ans)

 

728x90
300x250

'Baekjoon' 카테고리의 다른 글

백준 17413 단어 뒤집기 2 / Python  (0) 2023.03.22
백준 14499 주사위 굴리기 / Python  (0) 2023.03.21
백준 14503 로봇 청소기 / Python  (0) 2023.03.21
백준 5567 결혼식 / Python  (0) 2023.03.21
백준 5397 키로거 / Python  (0) 2023.03.20