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

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

14499๋ฒˆ: ์ฃผ์‚ฌ์œ„ ๊ตด๋ฆฌ๊ธฐ

์ฒซ์งธ ์ค„์— ์ง€๋„์˜ ์„ธ๋กœ ํฌ๊ธฐ N, ๊ฐ€๋กœ ํฌ๊ธฐ M (1 ≤ N, M ≤ 20), ์ฃผ์‚ฌ์œ„๋ฅผ ๋†“์€ ๊ณณ์˜ ์ขŒํ‘œ x y(0 ≤ x ≤ N-1, 0 ≤ y ≤ M-1), ๊ทธ๋ฆฌ๊ณ  ๋ช…๋ น์˜ ๊ฐœ์ˆ˜ K (1 ≤ K ≤ 1,000)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„์— ์ง€๋„

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 ์ดˆ๊ธฐ์— 6๋ฉด์ด 0์˜ ๊ฐ’์„ ๊ฐ€์ง€๋Š” ์ฃผ์‚ฌ์œ„๋ฅผ ๋™, ์„œ, ๋ถ, ๋‚จ์œผ๋กœ ์ง€๋„๋ฅผ ํƒ์ƒ‰ํ•  ๊ฒฝ์šฐ ํ˜„์žฌ ์ขŒํ‘œ์˜ ์ง€๋„ ๊ฐ’์ด 0์ด ์•„๋‹ˆ๋ฉด ์ฃผ์‚ฌ์œ„ ๋ฐ”๋‹ฅ์— ๊ฐ’์„ ๋ณต์‚ฌํ•˜๊ณ , ํ˜„์žฌ ์ขŒํ‘œ ์ง€๋„ ๊ฐ’์ด 0์ธ ๊ฒฝ์šฐ ์ฃผ์‚ฌ์œ„ ๋ฐ”๋‹ฅ์˜ ์ˆ˜๋ฅผ ์ง€๋„์— ๋ณต์‚ฌํ•œ๋‹ค. ์ด๋•Œ ๋ช…๋ น์— ๋”ฐ๋ผ ์ฃผ์‚ฌ์œ„๋ฅผ ์›€์ง์ผ ๋•Œ, ์ฃผ์‚ฌ์œ„ ์œ—๋ฉด์˜ ๊ฐ’์„ ์ถœ๋ ฅํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค.

 

 

 ์ฃผ์‚ฌ์œ„๋Š” ๋™, ์„œ, ๋ถ, ๋‚จ ๊ฐ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์ด ๋ณ€ํ™”ํ•œ๋‹ค. ๋™, ์„œ์ธ ๊ฒฝ์šฐ์—๋Š” ๊ธฐ์กด์˜ 4, 1, 3, 6๋ฒˆ์ด ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ˆœํšŒํ•˜๊ฑฐ๋‚˜ ์™ผ์ชฝ์œผ๋กœ ์ˆœํšŒํ•œ๋‹ค. ์ด์™€ ๋‹ฌ๋ฆฌ ๋ถ, ๋‚จ์ธ ๊ฒฝ์šฐ 2, 1, 5, 6๋ฒˆ์ด ์œ„๋‚˜ ์•„๋ž˜๋กœ ์ˆœํšŒํ•˜๊ฒŒ ๋œ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ๊ณ ๋ คํ•˜์—ฌ ์ฃผ์‚ฌ์œ„๊ฐ€ ํ•ด๋‹น ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ ์ฃผ์‚ฌ์œ„ ์ƒํƒœ๋ฅผ ๋ณ€ํ™”์‹œํ‚ค๋ฉด์„œ ํƒ์ƒ‰์„ ์ง„ํ–‰ํ•˜๋ฉด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฝ”๋“œ

from sys import stdin
from collections import deque


def move_dice(direction):
    if direction == E:
        dice[3], dice[1], dice[4], dice[6] = dice[1], dice[4], dice[6], dice[3]
    elif direction == W:
        dice[3], dice[1], dice[4], dice[6] = dice[6], dice[3], dice[1], dice[4]
    elif direction == N:
        dice[2], dice[1], dice[5], dice[6] = dice[1], dice[5], dice[6], dice[2]
    elif direction == S:
        dice[2], dice[1], dice[5], dice[6] = dice[6], dice[2], dice[1], dice[5]


def bfs(start):
    q = deque([start])
    # ๋™, ์„œ, ๋ถ, ๋‚จ
    dirs = [(), (0, 1), (0, -1), (-1, 0), (1, 0)]

    for order in orders:
        cur_x, cur_y = q.popleft()
        dx, dy = dirs[order]
        next_x, next_y = cur_x + dx, cur_y + dy

        if 0 <= next_x < n and 0 <= next_y < m:
            move_dice(order)
            if graph[next_x][next_y]:
                dice[6] = graph[next_x][next_y]
                graph[next_x][next_y] = 0
            else:
                graph[next_x][next_y] = dice[6]
            print(dice[1])
            q.append((next_x, next_y))
        else:
            q.append((cur_x, cur_y))


if __name__ == '__main__':
    E, W, N, S = 1, 2, 3, 4
    n, m, x, y, k = map(int, stdin.readline().split())
    graph = [list(map(int, stdin.readline().split())) for _ in range(n)]
    orders = list(map(int, stdin.readline().split()))
    # ์ฃผ์‚ฌ์œ„์˜ ์ธ๋ฑ์Šค๋Š” ๋ฌธ์ œ์— ์ฃผ์–ด์ง„ ๊ฒƒ๊ณผ ๋™์ผ.
    dice = [0] * 7
    bfs([x, y])

 ์˜ˆ์ œ ์ž…๋ ฅ ์ผ€์ด์Šค๋งŒ ๋ชจ๋‘ ์ž˜ ์ฒ˜๋ฆฌ ๋œ๋‹ค๋ฉด, ๋‹ต์•ˆ์„ ์ œ์ถœํ•˜์˜€์„ ๋•Œ ์ •๋‹ต์ด๋ผ๋Š” ๋ฌธ๊ตฌ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค. ๋™, ์„œ, ๋‚จ, ๋ถ์ด ์•„๋‹ˆ๋ผ ๋™, ์„œ, ๋ถ, ๋‚จ์ด๋ผ์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ ํ•œ๋ฒˆ ํ—ท๊ฐˆ๋ ธ๋‹ค. ๋˜ํ•œ, ์ฃผ์‚ฌ์œ„๋ฅผ ๋™, ์„œ๋กœ ์˜ฎ๊ธธ ๋•Œ ๋ฐ”๋‹ฅ๋ฉด๋„ ํฌํ•จํ•ด์„œ ์ˆœํšŒ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์ธ์ง€ํ•˜์ง€ ๋ชปํ•ด ํ•œ๋ฒˆ ๋” ํ—ท๊ฐˆ๋ ธ๋‹ค.๐Ÿ˜‚

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