3190-๋ฑ€

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

  1. ( x, y ) ๋Š” ํ˜„์žฌ ๋จธ๋ฆฌ์˜ ์œ„์น˜์ž…๋‹ˆ๋‹ค.

  2. ๋งค์ดˆ๋งˆ๋‹ค d ( ๋ฐฉํ–ฅ ) ์ด ๋ณ€ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๊ณ  ์ƒํ™ฉ์— ๋งž๊ฒŒ ์ ์šฉํ•ด์ค๋‹ˆ๋‹ค.

  3. ( nx, ny ) ๋Š” ๋‹ค์Œ ๋จธ๋ฆฌ๊ฐ€ ์œ„์น˜ํ•  ๊ณณ์ž…๋‹ˆ๋‹ค.

  4. q ๋Š” ํ˜„์žฌ ๋ฑ€์˜ ๋ชธ์ด ์กด์žฌํ•˜๋Š” ์œ„์น˜๋“ค์ž…๋‹ˆ๋‹ค.

  5. ๋งค์ดˆ๋งˆ๋‹ค ( nx, ny ) ์— ์ž์‹ ์˜ ๋ชธ์ด ์žˆ๊ฑฐ๋‚˜ ๋ฒฝ์ด๋ผ๋ฉด ์‹œ๊ฐ„์„ ์ถœ๋ ฅํ•˜๊ณ  ๋งˆ์นฉ๋‹ˆ๋‹ค.

  6. ( nx, ny ) ์— ์•„๋ฌด๊ฒƒ๋„ ์—†๋‹ค๋ฉด, q ๋ฅผ ์„ ์ž…์„ ์ถœํ•ด์ค๋‹ˆ๋‹ค.

  7. ( nx, ny ) ์— ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด, q ๋ฅผ ์ˆ˜์ •ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

import sys


def solution(n):
    time = 0
    y, x = 0, 0
    q = [(0, 0)]
    dy = [0, 1, 0, -1]
    dx = [1, 0, -1, 0]
    d = 0

    while True:
        # ๋ฐฉํ–ฅ ์„ค์ •
        for plan in dir_plan:
            if time == plan[0]:
                if plan[1] == 'D':
                    d = (d + 1) % 4
                else:
                    d = (d - 1) % 4

        # ๋‹ค์Œ ๋จธ๋ฆฌ ์œ„์น˜ ์„ค์ •
        ny = y + dy[d]
        nx = x + dx[d]

        # ๋‹ค์Œ ๋จธ๋ฆฌ๊ฐ€ ๋ชธ์ด๋‚˜ ๋ฒฝ์ด๋ผ๋ฉด time + 1 ์ถœ๋ ฅ
        if (ny,nx) in q or ny < 0 or ny >= n or nx < 0 or nx >= n:
            return print(time + 1)

        # ๋‹ค์Œ ๋จธ๋ฆฌ๊ฐ€ ์‚ฌ๊ณผ๋ฉด ์ž์ทจ์— ์ถ”๊ฐ€๋งŒ ์ง„ํ–‰
        elif arr[ny][nx] == 'apple':
            arr[ny][nx] = 0
            q.append((ny,nx))

        # ๋‹ค์Œ ๋จธ๋ฆฌ์— ์•„๋ฌด๊ฒƒ๋„ ์—†์œผ๋ฉด ์ž์ทจ์— ๋ชธ ์ถ”๊ฐ€ ํ•˜๊ณ  ๊ผฌ๋ฆฌ๋Š” ๋นผ์ž
        else:
            q.append((ny,nx))
            q.pop(0)

        # ์‹œ๊ฐ„ ์œ„์น˜ ์—…๋ฐ์ดํŠธ
        time += 1
        y = ny
        x = nx


n = int(input())
arr = [[0] * n for _ in range(n)]

k = int(input())
apples = [list(map(int,sys.stdin.readline().split())) for _ in range(k)]
for apple in apples:
    arr[apple[0] - 1][apple[1] - 1] = 'apple'

l = int(input())
dir_plan = [list(sys.stdin.readline().split()) for _ in range(l)]
for v in dir_plan:
    v[0] = int(v[0])

solution(n)

Last updated