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

BOJ 8972 - ๋ฏธ์นœ ์•„๋‘์ด๋…ธ

by HandHand 2021. 3. 8.

๋ฌธ์ œ

๋ฐฑ์ค€ ์˜จ๋ผ์ธ ์ €์ง€ - 8972๋ฒˆ

ํ’€์ด ๊ณผ์ •

ํ”Œ๋ ˆ์ด์–ด์™€ ๋ฏธ์นœ ์•„๋‘์ด๋…ธ์˜ ๋™์ž‘์„ ์‹œ๋ฎฌ๋ ˆ์ด์…˜ํ•˜๋Š” ๊ตฌํ˜„ ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

์กฐ๊ฑด์ด ๋งŽ์€ ๊ตฌํ˜„๋ฌธ์ œ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ชจ๋“ˆํ™”์— ์‹ ๊ฒฝ์„ ๋งŽ์ด ์จ์„œ ํ•ด๊ฒฐํ–ˆ์Šต๋‹ˆ๋‹ค.
ํ”Œ๋ ˆ์ด์–ด์™€ ๋กœ๋ด‡์˜ ์œ„์น˜๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๊ฐ์ฒด๋ฅผ ๊ฒŒ์ž„ํŒ๊ณผ ๋ณ„๋„๋กœ ์„ ์–ธํ•˜๊ณ  ๊ฒŒ์ž„ํŒ ์ด๋™, ๊ฒŒ์ž„ ์ข…๋ฅ˜ ์œ ๋ฌด๋ฅผ ํŒ๋‹จํ•ด์คฌ์Šต๋‹ˆ๋‹ค.
๋ฌธ์ œ ๊ตฌํ˜„ํ•˜๋ฉด์„œ python ์—์„œ ์ œ๊ณตํ•˜๋Š” any ํ•จ์ˆ˜๋ฅผ ์•Œ๊ฒŒ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.
Javascript ์˜ some ๊ณ ์ฐจํ•จ์ˆ˜์™€ ์œ ์‚ฌํ•œ๋ฐ ์ฝœ๋ฐฑ์„ ์ธ์ž๋กœ ๋ฐ›์ง€ ์•Š์•„ ์‚ฌ์šฉ๋ฐฉ๋ฒ•์ด ์ข€ ๋‹ค๋ฅด๋‹ค๋Š” ์ฐจ์ด์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

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

์ฝ”๋“œ


import sys
from collections import deque

R, C = list(map(int, sys.stdin.readline().split()))
board = [list(sys.stdin.readline().strip()) for _ in range(R)]
commands = list(map(int, sys.stdin.readline().strip()))

dx = [0, 1, 1, 1, 0, 0, 0, -1, -1, -1]
dy = [0, -1, 0, 1, -1, 0, 1, -1, 0, 1]


def pre_processing():
    game_map = [[deque() for _ in range(C)] for _ in range(R)]
    meta = {
        'I': [],
        'R': []
    }

    for x in range(R):
        for y in range(C):
            if not board[x][y] == '.':
                game_map[x][y].append(board[x][y])
                meta[board[x][y]].append([x, y])

    return meta, game_map


def move_jongsu(command, meta, game_map):
    x, y, = meta['I'][0]
    nx, ny = x + dx[command], y + dy[command]

    game_map[x][y].popleft()
    game_map[nx][ny].append('I')
    meta['I'] = [[nx, ny]]


def next_position(robot, jongsu):
    min_dist, position = sys.maxsize, None

    for i in range(1, 10):
        if i == 5:
            continue

        nx, ny = robot[0] + dx[i], robot[1] + dy[i]
        if 0 <= nx < R and 0 <= ny < C:
            dist = abs(jongsu[0] - nx) + abs(jongsu[1] - ny)
            if min_dist > dist:
                min_dist = dist
                position = [nx, ny]

    return position


def move_robots(meta, game_map):
    jongsu = meta['I'][0]
    robots = meta['R']
    moved_robots = []

    for robot in robots:
        next_xy = next_position(robot, jongsu)

        game_map[robot[0]][robot[1]].popleft()
        game_map[next_xy[0]][next_xy[1]].append('R')
        moved_robots.append(next_xy)

    meta['R'] = moved_robots


def allowed_to_continue(jongsu, game_map):
    x, y = jongsu
    if any(map(lambda e: e == 'R', game_map[x][y])):
        return False

    return True


def explode_robots(game_map, meta):
    survived_robots = []

    for x in range(R):
        for y in range(C):
            if len(game_map[x][y]) > 0:
                if game_map[x][y][0] == 'R':
                    if len(game_map[x][y]) == 1:
                        survived_robots.append([x, y])
                    else:
                        game_map[x][y].clear()

    meta['R'] = survived_robots


def run(command, meta, game_map):
    move_jongsu(command, meta, game_map)
    if not allowed_to_continue(meta['I'][0], game_map):
        return False

    move_robots(meta, game_map)
    if not allowed_to_continue(meta['I'][0], game_map):
        return False

    explode_robots(game_map, meta)

    return True


def solution():
    meta, game_map = pre_processing()

    for turn, command in enumerate(commands):
        survive = run(command, meta, game_map)
        if not survive:
            print(f"kraj {turn + 1}")
            return

    for x in range(R):
        for y in range(C):
            token = '.' if not game_map[x][y] else game_map[x][y][0]
            sys.stdout.write(f"{token}")
        sys.stdout.write("\n")


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

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

BOJ 1806 - ๋ถ€๋ถ„ํ•ฉ  (0) 2021.03.08
BOJ 6186 - Best Grass  (0) 2021.03.08
BOJ 19941 - ํ–„๋ฒ„๊ฑฐ ๋ถ„๋ฐฐ  (0) 2021.03.08
BOJ 16469 - ์†Œ๋…„ ์ ํ”„  (0) 2021.03.05
BOJ 13565 - ์นจํˆฌ  (0) 2021.03.05

๐Ÿ’ฌ ๋Œ“๊ธ€