Algorithm

[๋ฐฑ์ค€] 3190. ๋ฑ€ python

๐Ÿณ ๋งํฌ

๋ฐฑ์ค€ 3190๋ฒˆ ๋ฑ€

๐Ÿ• ๋ฌธ์ œ

'Dummy' ๋ผ๋Š” ๋„์Šค๊ฒŒ์ž„์ด ์žˆ๋‹ค. ์ด ๊ฒŒ์ž„์—๋Š” ๋ฑ€์ด ๋‚˜์™€์„œ ๊ธฐ์–ด๋‹ค๋‹ˆ๋Š”๋ฐ, ์‚ฌ๊ณผ๋ฅผ ๋จน์œผ๋ฉด ๋ฑ€ ๊ธธ์ด๊ฐ€ ๋Š˜์–ด๋‚œ๋‹ค. ๋ฑ€์ด ์ด๋ฆฌ์ €๋ฆฌ ๊ธฐ์–ด๋‹ค๋‹ˆ๋‹ค๊ฐ€ ๋ฒฝ ๋˜๋Š” ์ž๊ธฐ์ž์‹ ์˜ ๋ชธ๊ณผ ๋ถ€๋”ชํžˆ๋ฉด ๊ฒŒ์ž„์ด ๋๋‚œ๋‹ค.

๊ฒŒ์ž„์€ NxN ์ •์‚ฌ๊ฐ ๋ณด๋“œ์œ„์—์„œ ์ง„ํ–‰๋˜๊ณ , ๋ช‡๋ช‡ ์นธ์—๋Š” ์‚ฌ๊ณผ๊ฐ€ ๋†“์—ฌ์ ธ ์žˆ๋‹ค. ๋ณด๋“œ์˜ ์ƒํ•˜์ขŒ์šฐ ๋์— ๋ฒฝ์ด ์žˆ๋‹ค. ๊ฒŒ์ž„์ด ์‹œ์ž‘ํ• ๋•Œ ๋ฑ€์€ ๋งจ์œ„ ๋งจ์ขŒ์ธก์— ์œ„์น˜ํ•˜๊ณ  ๋ฑ€์˜ ๊ธธ์ด๋Š” 1 ์ด๋‹ค. ๋ฑ€์€ ์ฒ˜์Œ์— ์˜ค๋ฅธ์ชฝ์„ ํ–ฅํ•œ๋‹ค.

๋ฑ€์€ ๋งค ์ดˆ๋งˆ๋‹ค ์ด๋™์„ ํ•˜๋Š”๋ฐ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ทœ์น™์„ ๋”ฐ๋ฅธ๋‹ค.

๋จผ์ € ๋ฑ€์€ ๋ชธ๊ธธ์ด๋ฅผ ๋Š˜๋ ค ๋จธ๋ฆฌ๋ฅผ ๋‹ค์Œ์นธ์— ์œ„์น˜์‹œํ‚จ๋‹ค.
๋งŒ์•ฝ ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด, ๊ทธ ์นธ์— ์žˆ๋˜ ์‚ฌ๊ณผ๊ฐ€ ์—†์–ด์ง€๊ณ  ๊ผฌ๋ฆฌ๋Š” ์›€์ง์ด์ง€ ์•Š๋Š”๋‹ค.
๋งŒ์•ฝ ์ด๋™ํ•œ ์นธ์— ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด, ๋ชธ๊ธธ์ด๋ฅผ ์ค„์—ฌ์„œ ๊ผฌ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•œ ์นธ์„ ๋น„์›Œ์ค€๋‹ค. ์ฆ‰, ๋ชธ๊ธธ์ด๋Š” ๋ณ€ํ•˜์ง€ ์•Š๋Š”๋‹ค.
์‚ฌ๊ณผ์˜ ์œ„์น˜์™€ ๋ฑ€์˜ ์ด๋™๊ฒฝ๋กœ๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ์ด ๊ฒŒ์ž„์ด ๋ช‡ ์ดˆ์— ๋๋‚˜๋Š”์ง€ ๊ณ„์‚ฐํ•˜๋ผ.

๐Ÿพ ํ’€์ด ๋ฐฉ๋ฒ•

๋จผ์ €, n*n ํฌ๊ธฐ์˜ board๋ฅผ ๋ชจ๋‘ 0์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•ด์ค€๋‹ค. ์ดํ›„, ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋Š” ์œ„์น˜๋งŒ 1๋กœ ํ‘œ์‹œํ•จ์œผ๋กœ์จ ์‚ฌ๊ณผ๊ฐ€ ์žˆ์Œ์„ ๋‚˜ํƒ€๋‚ด์ค€๋‹ค.
๋˜ํ•œ ๋ฑ€์ด ์ž์‹ ์˜ ๋ชธ์— ๋‹ฟ์•„๋„ ์ข…๋ฃŒํ•˜๋ฏ€๋กœ ๋ฑ€์˜ ๋ชธ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ ์œ„ํ•ด snake๋ผ๋Š” ๋ฐฐ์—ด์„ ํ™œ์šฉํ•˜์—ฌ ๋ฑ€์˜ ๋ชธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋งŒ 1๋กœ ๋‚˜ํƒ€๋‚ธ๋‹ค.

๋ฑ€์€ ์ฒ˜์Œ์—๋Š” [0,1] ์ฆ‰, ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋‹ค ์ผ์ • ์‹œ๊ฐ„ ์ดํ›„ ๋“ค์–ด์˜จ ์ž…๋ ฅ์„ ํ†ตํ•ด ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ฒ˜์Œ์˜ direction์€ [0,1]๋กœ ์„ค์ •ํ•˜์—ฌ ์ด๋™ํ•œ๋‹ค. ์ดํ›„ ๋ฐฉํ–ฅ์„ ๋ณ€๊ฒฝํ•˜๋ผ๋Š” L์ด ์ž…๋ ฅ๋˜์—ˆ์„ ๋•Œ, Rotation ํ•จ์ˆ˜๋ฅผ ํ™œ์šฉํ•˜์—ฌ dir๋ฅผ ๋ณ€๊ฒฝํ•ด์ค€๋‹ค.

์ด๋•Œ Rotation ํ•จ์ˆ˜๋Š”, ํšŒ์ „๋ณ€ํ™˜ํ–‰๋ ฌ์„ ์ด์šฉํ•˜์—ฌ ์ƒˆ๋กœ์šด dir๋ฅผ ๊ณ„์‚ฐํ•ด์ค€๋‹ค. ํšŒ์ „๋ณ€ํ™˜ํ–‰๋ ฌ์€ R(theta) = [[cos, -sin], [sin, cos]] ์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ํ–‰๋ ฌ์ด๋‹ค.

๋ฑ€์ด ์ด๋™ํ•˜๋‹ค๊ฐ€ ์‚ฌ๊ณผ๋ฅผ ๋ฐœ๊ฒฌํ•œ ๊ฒฝ์šฐ์—๋Š” ๊ผฌ๋ฆฌ๋ฅผ ์ค„์ด์ง€ ์•Š๊ณ , ์‚ฌ๊ณผ๋ฅผ ๋ฐœ๊ฒฌํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋Š” ๊ผฌ๋ฆฌ๋ฅผ ์ค„์ด๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ๋งˆ์ง€๋ง‰ ๊ผฌ๋ฆฌ์˜ ์œ„์น˜๋ฅผ ์ €์žฅํ•˜๋Š” tail์ด๋ผ๋Š” ๋ฐฐ์—ด์„ ์„ ์–ธํ•˜์—ฌ, ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰ ๊ผฌ๋ฆฌ๋ฅผ ์‚ญ์ œํ•ด์ฃผ๋„๋ก ํ•œ๋‹ค.

๋ชจ๋“  ํ…Œ์ŠคํŠธ์ผ€์ด์Šค ์„ฑ๊ณต ์ฝ”๋“œ

import sys
import math
sys.stdin = open("input.txt","rt")
def R(x):
    return [[round(math.cos(x)), -round((math.sin(x)))],[round(math.sin(x)), round(math.cos(x))]]

def Rotation(str, d):
    result = []
    if str == 'L': #left
        rMatrix = R(math.radians(90))
    elif str == 'D': #right
        rMatrix = R(math.radians(-90))

    for i in range(len(rMatrix)):
        tmpVal = 0
        for j in range(len(rMatrix[0])):
            tmpVal += rMatrix[i][j] * d[j]
        result.append(tmpVal)

    return result

n = int(input()) #๋ณด๋“œ์˜ ํฌ๊ธฐ
board = [[0] * n for _ in range(n)]
snake = [[0] * n for _ in range(n)]
snake[0][0] = 1

k = int(input()) #์‚ฌ๊ณผ์˜ ๊ฐœ์ˆ˜ K
for _ in range(k):
    apple = list(map(int,input().split()))
    board[apple[0]-1][apple[1]-1] = 1 #์‚ฌ๊ณผ๊ฐ€ ์žˆ๋Š” ์ž๋ฆฌ๋Š” 1๋กœ ํ‘œ์‹œ

l = int(input())
going = list()
for _ in range(l):
    going.append(list(input().split()))

time,rotation = going.pop(0)
time = int(time)

#๋ฑ€์˜ ๋จธ๋ฆฌ(์ด๋™)
x = 0
y = 0

dir = [0, 1] #์ด๋™ํ•˜๋Š” ๋ฐฉํ–ฅ
second = 1 #์‹œ๊ฐ„ ์ดˆ
tail = list() #๋ฑ€์˜ ๊ผฌ๋ฆฌ

while True:
    if second - 1 == time:
        dir = Rotation(rotation, dir)
        if going:
            time, rotation = going.pop(0)
            time = int(time)
        else:
            time = -1
    tail.append([x,y])
    x = x + dir[0]
    y = y + dir[1]
    if (0 > x or n <= x) or (0 > y or n <= y):
        break
    if snake[x][y] == 1: #์ด๋ฏธ ๋ฑ€์˜ ๋ชธ์ด ์žˆ๋Š” ๊ฒฝ์šฐ
        break

    if board[x][y] == 0: #์‚ฌ๊ณผ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ
        s_x, s_y = tail.pop(0)
        snake[s_x][s_y] = 0 #๊ผฌ๋ฆฌ๋ฅผ ํ•œ์นธ ์˜ฎ๊น€
    else:
        board[x][y] = 0 #์‚ฌ๊ณผ๋ฅผ ๋จน์—ˆ์œผ๋ฏ€๋กœ 0์œผ๋กœ ๋ฐ”๊พธ์–ด์คŒ

    snake[x][y] = 1
    second += 1

print(second)