본문 바로가기
{ Problem Solving }/Implementation

[구현 문제] 백준 3190번: 뱀 / 골드 5

by ggyongi 2021. 6. 7.
반응형

 

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

 

3190번: 뱀

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

www.acmicpc.net

import collections
n = int(input())
k = int(input())

apples = collections.defaultdict(int)
for i in range(k):
    x, y = map(int, input().split())
    x -= 1
    y -= 1
    apples[1000*x+y] = 1

t = int(input())
turns = {}
for i in range(t):
    a, b = input().split()
    turns[int(a)] = b

d = 0
dx = [0,1,0,-1]
dy = [1,0,-1,0]

x, y = 0, 0
body = [[x, y]]
time = 0
while True:
    # move forward
    x += dx[d]
    y += dy[d]

    # finish check
    if [x,y] in body:
        break
    if x<0 or x>=n or y<0 or y>=n: # touch wall
        break

    body.append([x,y])

    # apple check
    if apples[1000*x+y]==1:
        apples[1000*x+y] = 0
    else:
        body.pop(0)

    time += 1
    if time in turns:
        if turns[time] == "D": # turn right
            d += 1
            d = d % 4
        else:
            d += 3
            d = d % 4
print(time+1)

전형적인 구현 문제다!

 

 

비전공자 네카라 신입 취업 노하우

시행착오 끝에 얻어낸 취업 노하우가 모두 담긴 전자책!

kmong.com

댓글