ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

3190๋ฒˆ: ๋ฑ€

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

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

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

 

 ๋ฌธ์ œ๋Š” BFS๋ฅผ ํ†ตํ•ด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ํƒ์ƒ‰ํ•˜๋ฉด ๋œ๋‹ค.

  • ๋ฑ€์€ ์˜ค๋ฅธ์ชฝ์„ ํ–ฅํ•˜๋ฏ€๋กœ (0, 0)์—์„œ (0, 1)์„ ๋”ํ•˜๊ฐ€๋ฉฐ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.
  • ํŠน์ • ์‹œ๊ฐ„ ์ดˆ๊ฐ€ ๋˜๋ฉด, 'L', 'D'์— ๋”ฐ๋ผ ์ „ํ™˜ํ•  ๋ฐฉํ–ฅ์„ ์„ ํƒํ•˜๊ณ  ๊ทธ ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค.
  • ๋ฑ€์˜ ๊ผฌ๋ฆฌ๋ฅผ ๋‹ค์‹œ ๋งŒ๋‚˜๊ฑฐ๋‚˜, NxN ์ •์‚ฌ๊ฐ ๋ณด๋“œ ๋ฐ–์œผ๋กœ ๋‚˜๊ฐˆ ๊ฒฝ์šฐ ๊ฒŒ์ž„์ด ์ค‘๋‹จ ๋œ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def change_dir(cur_dir, change):
    return (cur_dir - 1 if change == 'L' else cur_dir + 1) % 4


def visitable(x, y):
    return 0 <= x < n and 0 <= y < n and graph[x][y] != SNAKE


def bfs(start):
    q = deque([start])
    time = 0
    cur_dir = 0
    x, y = 0, 0
    graph[x][y] = SNAKE

    while True:
        if move and move[-1][0] == time:
            cur_dir = change_dir(cur_dir, move.pop()[1])

        next_x, next_y = x + dirs[cur_dir][0], y + dirs[cur_dir][1]

        time += 1

        if visitable(next_x, next_y):
            if graph[next_x][next_y] == APPLE:
                graph[next_x][next_y] = SNAKE
                q.append((next_x, next_y))
            elif graph[next_x][next_y] == EMPTY:
                graph[next_x][next_y] = SNAKE
                q.append((next_x, next_y))
                tail_x, tail_y = q.popleft()
                graph[tail_x][tail_y] = EMPTY
            x, y = next_x, next_y
        else:
            break
    return time


if __name__ == '__main__':
    EMPTY, SNAKE, APPLE = 0, 1, 2
    n = int(stdin.readline())
    graph = [[0] * n for _ in range(n)]
    dirs = [[0, 1], [1, 0], [0, -1], [-1, 0]]
    move = []

    for _ in range(int(stdin.readline())):
        i, j = map(int, stdin.readline().split())
        graph[i - 1][j - 1] = APPLE

    for _ in range(int(stdin.readline())):
        move_time, move_dir = stdin.readline().split()
        move.append([int(move_time), move_dir])

    move = move[::-1]

    print(bfs([0, 0]))

 `deque`๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฑ€์˜ ๊ธธ์ด๋ฅผ ์œ ์ง€ํ•˜๋ฉด์„œ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•œ๋‹ค. ์‚ฌ๊ณผ๋ฅผ ๋จน์ง€ ๋ชปํ•  ๊ฒฝ์šฐ ๋ฑ€์˜ ํฌ๊ธฐ๋ฅผ ์ค„์ด๊ณ  (`q.popleft`), ์‚ฌ๊ณผ๋ฅผ ๋จน์„ ๊ฒฝ์šฐ ๋ฑ€์˜ ํฌ๊ธฐ๋ฅผ ๋Š˜๋ฆฐ๋‹ค. ๋˜ํ•œ, ๋ฑ€์˜ ๋จธ๋ฆฌ๊ฐ€ ๊ผฌ๋ฆฌ๋ฅผ ๋ฐŸ๋Š” ๊ฒฝ์šฐ๋ฅผ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด, ๋ณด๋“œ์˜ ๊ฐ’์„ ๋ฑ€์œผ๋กœ ์ˆ˜์ •ํ•˜์—ฌ ํ™•์ธํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.๐Ÿ˜‰

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€