๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿƒ algorithm/boj

BOJ 3190 - ๋ฑ€

by HandHand 2021. 3. 1.

๋ฌธ์ œ

[๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 3190๋ฒˆ](https://www.acmicpc.net/problem/3190)

ํ’€์ด ๊ณผ์ •

๋ฑ€ ๊ฒŒ์ž„์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•ด๋ณด๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์ฃผ์˜ํ•  ์ ์€ N์ดˆ ํ›„์— ํšŒ์ „์„ ํ•œ๋‹ค๋Š” ๊ฒƒ์ด N+1 ์ดˆ์— ํšŒ์ „๋œ ์œ„์น˜๋กœ์˜ ์ด๋™์ด ์ด๋ฃจ์–ด์ง„๋‹ค๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฒ˜์Œ ํ’€๋•Œ๋Š” ์กฐ๊ธˆ ์ง€์ €๋ถ„ํ•˜๊ฒŒ ํ‘ผ ๊ฒƒ ๊ฐ™์•„์„œ ๋‹ค์‹œ ํ’€์–ด๋ดค์Šต๋‹ˆ๋‹ค.

์ฒซ๋ฒˆ์งธ ์‹œ๋„

๊ฒŒ์ž„ํŒ ์„ค์ •

์šฐ์„  ๋ฑ€ ๊ฒŒ์ž„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์„ ์œ„ํ•ด (N+2)\*(N+2) ํฌ๊ธฐ์˜ ๊ฒŒ์ž„ํŒ board ๋ฅผ ์„ ์–ธํ•˜์˜€๋‹ค.

๋ฌธ์ œ ์กฐ๊ฑด์—์„œ ๊ฐ€์žฅ์ž๋ฆฌ๋Š” ๋ฒฝ์œผ๋กœ ํ•˜๊ณ  ๊ฒŒ์ž„ ์‹œ์ž‘์ง€์ ์„ 1ํ–‰ 1์—ด๋กœ ํ•˜์˜€์œผ๋ฏ€๋กœ ๊ตฌํ˜„์˜ ํŽธ์˜์„ฑ์„ ์œ„ํ•ด ๊ฐ€์žฅ์ž๋ฆฌ๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒƒ์ด๋‹ค.

๋˜ํ•œ ํ˜„์žฌ ๊ฒŒ์ž„ํŒ์˜ ์ƒํƒœ๋ฅผ ํ‘œํ˜„ํ•ด์ฃผ๋Š” ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ง๊ด€์ ์ธ ์ฝ”๋“œ ์ž‘์„ฑ์„ ๊พ€ํ•˜์˜€๋‹ค.

๋ฑ€์˜ ์ด๋™

๊ฐ€์žฅ ๊นŒ๋‹ค๋กœ์šด ๋ถ€๋ถ„์€ ๋ฑ€์˜ ์ด๋™์—ฐ์‚ฐ์ž…๋‹ˆ๋‹ค.

์ด๋ฅผ ์œ„ํ•ด์„œ๋Š” ํ˜„์žฌ ๋ฑ€์˜ ์œ„์น˜์™€ ๋ฐ”๋€” ํšŒ์ „ ๋ฐฉํ–ฅ์„ ๊ณ ๋ คํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

๋˜ํ•œ ์ด๋™์‹œ ๋ฑ€์˜ ๊ผฌ๋ฆฌ๋„ ๋”ฐ๋ผ์™€์•ผ ํ•˜๋ฏ€๋กœ ๊ต‰์žฅํžˆ ๋ณต์žกํ•œ ์—ฐ์‚ฐ์ฒ˜๋Ÿผ ๋Š๊ปด์ง‘๋‹ˆ๋‹ค.

์šฐ์„  ๊ฐ ๋ฐฉํ–ฅ(๋™, ์„œ, ๋‚จ, ๋ถ)์— ๋”ฐ๋ฅธ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํ˜„์žฌ ๋ฐฉํ–ฅ์— ๋”ฐ๋ผ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ํšŒ์ „ ๊ฒฐ๊ณผ๋Š” ๋‹ค๋ฅด๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ฅผ ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์— ์ €์žฅํ•œ ๋’ค ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋ฑ€์˜ ์ด๋™์„ ๊ตฌํ˜„ํ•˜๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๊ฒ ์ง€๋งŒ ๋‚˜๋Š” ๋งค ์ดˆ๋งˆ๋‹ค ํšŒ์ „ ์—ฐ์‚ฐ ๋ชฉ๋ก์„ ํ™•์ธํ•ด์ฃผ๋ฉฐ

์‹œ๊ฐ„์ด ๋˜์—ˆ์„ ๋•Œ ํ•ด๋‹นํ•˜๋Š” ํšŒ์ „ ์—ฐ์‚ฐ์ด ์ด๋ฃจ์–ด์ง€๋„๋ก ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ๋จธ๋ฆฌ์™€ ๊ผฌ๋ฆฌ์˜ ์ด๋™ ๋ฐฉํ–ฅ์„ ๊ฐ๊ฐ ๋”ฐ๋กœ ์œ ์ง€ํ•˜๋ฉด์„œ ํšŒ์ „ ์—ฐ์‚ฐ์ด ์ด๋ฃจ์–ด์กŒ์„ ๊ฒฝ์šฐ ํ˜„์žฌ ์œ„์น˜์™€ ๋‹ค์Œ ์ด๋™ ๋ฐฉํ–ฅ์„ ํ์— ์ €์žฅํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

์–ด์ฐจํ”ผ ๊ผฌ๋ฆฌ๋Š” ํ˜„์žฌ ๋ฐฉํ–ฅ์„ ๊ธฐ์ค€์œผ๋กœ ํ•œ ์นธ์”ฉ ์ด๋™ํ•˜๋‹ˆ๊นŒ ๋งŒ์•ฝ ๊ผฌ๋ฆฌ๊ฐ€ ์ด์ „์— ํ•œ๋ฒˆ ๊บพ์ธ ์ง€์ ์— ๋„๋‹ฌํ–ˆ์„๋•Œ ์ด์ „์— ํšŒ์ „ํ–ˆ๋˜ ๋ฐฉํ–ฅ์œผ๋กœ ๊ผฌ๋ฆฌ๋„ ํšŒ์ „ํ•ด์ฃผ๋„๋ก ํ•˜๋ฉด ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ฝ”๋“œ

์ฒซ๋ฒˆ์งธ ์‹œ๋„

import sys
from collections import deque


N = int(input())
K = int(input())
apples = []
for _ in range(K):
    apples.append(list(map(int, sys.stdin.readline().split())))
L = int(input())
rotate = []
for _ in range(L):
    line = list(sys.stdin.readline().split())
    rotate.append([int(line[0]), line[1]])


state = {
    'blank': 0,
    'wall': 1,
    'body': 2,
    'apple': 3,
}

# ํ˜„์žฌ ๋ฐฉํ–ฅ์„ ๊ณ ๋ คํ•œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์ด๋™์‹œ ๋‹ค์Œ ๋ฐฉํ–ฅ
next_rotate_dir = {
    'e': {'L': 'n', 'D': 's'},
    'w': {'L': 's', 'D': 'n'},
    's': {'L': 'e', 'D': 'w'},
    'n': {'L': 'w', 'D': 'e'},
}

# ๋™์„œ๋‚จ๋ถ ์ด๋™ ์ขŒํ‘œ
move_pos = {
    'e': [0, 1],
    'w': [0, -1],
    's': [1, 0],
    'n': [-1, 0],
}

def solution():
    timer = 0
    dir = 'e'
    tail_dir = 'e'
    head = [1, 1]
    tail = [1, 1]
    board = [[0 for _ in range(N+2)] for _ in range(N+2)]
    rotate.reverse()

    rotate_log = deque()

    for i in range(N + 2):
        board[0][i] = 1
        board[i][0] = 1
        board[i][N+1] = 1
        board[N+1][i] = 1

    for apple in apples:
        board[apple[0]][apple[1]] = state['apple']

    board[1][1] = state['body']

    while True:
        # ๋ณ€ํ™˜ ์‹œ๊ฐ„์ด ๋˜๋ฉด
        if rotate and timer == rotate[-1][0]:
            dir = next_rotate_dir[dir][rotate[-1][1]]
            rotate_log.append((head, dir))  # ๋ณ€ํ™˜ ์ง€์ ๊ณผ ๋‹ค์Œ ๋ฐฉํ–ฅ์„ ๋กœ๊ทธ์— ์ €์žฅ
            rotate.pop()

        next_head = [head[0] + move_pos[dir][0], head[1] + move_pos[dir][1]]
        head = next_head

        # ์ž์‹ ์˜ ๋ชธ ํ˜น์€ ๋ฒฝ์— ๋จธ๋ฆฌ๊ฐ€ ๋‹ฟ์œผ๋ฉด ์ข…๋ฃŒ
        if board[head[0]][head[1]] == state['wall'] or board[head[0]][head[1]] == state['body']:
            timer += 1
            break
        # ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด
        elif board[head[0]][head[1]] == state['apple']:
            board[head[0]][head[1]] = state['body']
        # ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด
        else:
            board[head[0]][head[1]] = state['body']
            board[tail[0]][tail[1]] = state['blank']

            # ๋‹ค์Œ ๊ผฌ๋ฆฌ ์œ„์น˜ ์ฐพ๊ธฐ
            # ํ˜„์žฌ ๊ผฌ๋ฆฌ๊ฐ€ ์ด์ „์— ๊บพ์ธ ์ง€์ ์ด๋ฉด
            if rotate_log and tail == rotate_log[0][0]:
                tail_dir = rotate_log[0][1]
                rotate_log.popleft()
            next_tail = [tail[0] + move_pos[tail_dir][0], tail[1] + move_pos[tail_dir][1]]
            tail = next_tail
        timer += 1

    return timer


print(solution())

๋‘๋ฒˆ์งธ ์‹œ๋„

๋ฑ€์„ deque ์— ์ €์žฅํ•œ ๋’ค ๊ฐ ์‹œ๊ฐ„๋งˆ๋‹ค ๋จธ๋ฆฌ๋ฅผ ๊บผ๋‚ด์„œ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ์Šต๋‹ˆ๋‹ค.

๋˜ํ•œ ๊ฐ ํšŒ์ „ ๋ฐฉํ–ฅ์„ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ‘œํ˜„ํ•˜์ง€ ์•Š๊ณ  ๊ทธ๋ƒฅ ๋ฐฐ์—ด์— ์„ ์–ธํ•ด์คฌ์Šต๋‹ˆ๋‹ค.

import sys
from collections import deque


N = int(input())
K = int(input())
apples = []
for _ in range(K):
    apples.append(list(map(int, sys.stdin.readline().split())))
L = int(input())
rotate = []
for _ in range(L):
    line = list(sys.stdin.readline().split())
    rotate.append([int(line[0]), line[1]])


state = {
    'blank': 0,
    'wall': 1,
    'body': 2,
    'apple': 3,
}

# ํ˜„์žฌ ๋ฐฉํ–ฅ์„ ๊ณ ๋ คํ•œ ์™ผ์ชฝ๊ณผ ์˜ค๋ฅธ์ชฝ ์ด๋™์‹œ ๋‹ค์Œ ๋ฐฉํ–ฅ
next_rotate_dir = {
    'e': {'L': 'n', 'D': 's'},
    'w': {'L': 's', 'D': 'n'},
    's': {'L': 'e', 'D': 'w'},
    'n': {'L': 'w', 'D': 'e'},
}

# ๋™์„œ๋‚จ๋ถ ์ด๋™ ์ขŒํ‘œ
move_pos = {
    'e': [0, 1],
    'w': [0, -1],
    's': [1, 0],
    'n': [-1, 0],
}

def solution():
    timer = 0
    dir = 'e'
    tail_dir = 'e'
    head = [1, 1]
    tail = [1, 1]
    board = [[0 for _ in range(N+2)] for _ in range(N+2)]
    rotate.reverse()

    rotate_log = deque()

    for i in range(N + 2):
        board[0][i] = 1
        board[i][0] = 1
        board[i][N+1] = 1
        board[N+1][i] = 1

    for apple in apples:
        board[apple[0]][apple[1]] = state['apple']

    board[1][1] = state['body']

    while True:
        # ๋ณ€ํ™˜ ์‹œ๊ฐ„์ด ๋˜๋ฉด
        if rotate and timer == rotate[-1][0]:
            dir = next_rotate_dir[dir][rotate[-1][1]]
            rotate_log.append((head, dir))  # ๋ณ€ํ™˜ ์ง€์ ๊ณผ ๋‹ค์Œ ๋ฐฉํ–ฅ์„ ๋กœ๊ทธ์— ์ €์žฅ
            rotate.pop()

        next_head = [head[0] + move_pos[dir][0], head[1] + move_pos[dir][1]]
        head = next_head

        # ์ž์‹ ์˜ ๋ชธ ํ˜น์€ ๋ฒฝ์— ๋จธ๋ฆฌ๊ฐ€ ๋‹ฟ์œผ๋ฉด ์ข…๋ฃŒ
        if board[head[0]][head[1]] == state['wall'] or board[head[0]][head[1]] == state['body']:
            timer += 1
            break
        # ์‚ฌ๊ณผ๊ฐ€ ์žˆ๋‹ค๋ฉด
        elif board[head[0]][head[1]] == state['apple']:
            board[head[0]][head[1]] = state['body']
        # ์‚ฌ๊ณผ๊ฐ€ ์—†๋‹ค๋ฉด
        else:
            board[head[0]][head[1]] = state['body']
            board[tail[0]][tail[1]] = state['blank']

            # ๋‹ค์Œ ๊ผฌ๋ฆฌ ์œ„์น˜ ์ฐพ๊ธฐ
            # ํ˜„์žฌ ๊ผฌ๋ฆฌ๊ฐ€ ์ด์ „์— ๊บพ์ธ ์ง€์ ์ด๋ฉด
            if rotate_log and tail == rotate_log[0][0]:
                tail_dir = rotate_log[0][1]
                rotate_log.popleft()
            next_tail = [tail[0] + move_pos[tail_dir][0], tail[1] + move_pos[tail_dir][1]]
            tail = next_tail
        timer += 1

    return timer


print(solution())
๋ฐ˜์‘ํ˜•

'๐Ÿƒ algorithm > boj' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ  (0) 2021.03.08
BOJ 16469 - ์†Œ๋…„ ์ ํ”„  (0) 2021.03.05
BOJ 13565 - ์นจํˆฌ  (0) 2021.03.05
BOJ 1105 - ํŒ”  (0) 2021.03.03
BOJ 16509 - ์žฅ๊ตฐ  (0) 2021.03.03

๐Ÿ’ฌ ๋Œ“๊ธ€