2020.02.09
CHAPTER12 구현 문제
삼성전자 SW 역량테스트
import sys
sys.stdin = open('input.txt', 'r')
from collections import deque as dq
# 이동 위치 Tuple
dy = (0, 1, 0, -1)
dx = (1, 0, -1, 0)
# NxN map (0:빈칸, 1:사과, 2:뱀), 0,0 에서 뱀 시작
N = int(input())
myMap = [[0 for _ in range(N)] for _ in range(N)]
myMap[0][0] = 2
# 사과 위치 놓기
K = int(input())
for _ in range(K):
y, x = map(int, input().split())
myMap[y-1][x-1] = 1
# 뱀 Deque 생성
Snake = dq()
Snake.append((0,0))
sizeSnake = 1
dirSnake = 0
# 위치정보 Dictionary
L = int(input())
dirDict = {}
for _ in range(L):
dirKey, dirValue = input().split()
dirDict[int(dirKey)+1] = dirValue
def solution():
global myMap, dirDict, Snake, sizeSnake, dirSnake
ret = 0
while True:
ret += 1
headY, headX = Snake[sizeSnake-1]
# 이동방향 정하기
if ret in dirDict.keys():
if dirDict[ret] == 'L':
dirSnake = ((dirSnake - 1) + 4) % 4
else:
dirSnake = (dirSnake + 1) % 4
# Head로부터 다음위치 설정
nextY, nextX = headY + dy[dirSnake], headX + dx[dirSnake]
# 게임종료조건
if nextY < 0 or nextX < 0 or nextY >= N or nextX >= N or myMap[nextY][nextX] == 2:
return ret
# 다음 칸 이동할 때 동작
elif myMap[nextY][nextX] == 1:
sizeSnake += 1
else:
tailY, tailX = Snake.popleft()
myMap[tailY][tailX] = 0
Snake.append((nextY, nextX))
myMap[nextY][nextX] = 2
return ret
print(solution())
Comment