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

BOJ 20005 - ๋ณด์Šค๋ชฌ์Šคํ„ฐ ์ „๋ฆฌํ’ˆ

by HandHand 2021. 3. 14.

๋ฌธ์ œ

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

ํ’€์ด ๊ณผ์ •

ํ•ด๋‹น ๋ฌธ์ œ๋Š” pypy3๋กœ ํ’€์ดํ–ˆ์Šต๋‹ˆ๋‹ค.

๋ชฌ์Šคํ„ฐ์™€ ํ”Œ๋ ˆ์ด์–ด์˜ ์œ„์น˜๊ฐ€ ์ฃผ์–ด์งˆ ๋•Œ ๋ชฌ์Šคํ„ฐ๋ฅผ ์ฒ˜์น˜ํ•ด์„œ ๋ณด์ƒ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ํ”Œ๋ ˆ์ด์–ด์˜ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ž…๋‹ˆ๋‹ค.

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


S[x][y][player] = (x, y) ์ง€์ ์— player ๊ฐ€ ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ์ ์ด ์žˆ๋Š”์ง€ ์œ ๋ฌด


ํ•œ ๊ฐ€์ง€ ์‹ค์ˆ˜๋ฅผ ํ–ˆ๋˜ ์ ์€ `BFS` ๊ฐ€ ์ข…๋ฃŒ๋˜๊ธฐ ์œ„ํ•œ ์กฐ๊ฑด์œผ๋กœ `queue` ๊ฐ€ ๋น„์–ด์žˆ๋Š”์ง€๋ฅผ ์ฒดํฌํ•œ ๊ฒƒ์ธ๋ฐ,
์‚ฌ์‹ค ๋” ์ด์ƒ ์ด๋™ํ•  ๊ณณ์ด ์—†๋”๋ผ๋„ ๋ชฌ์Šคํ„ฐ์˜ ์ฒด๋ ฅ์ด 0 ์ดํ•˜๊ฐ€ ๋  ๋•Œ๊นŒ์ง€ ๊ฒŒ์ž„์„ ์ง„ํ–‰ํ•ด์•ผํ•ด์„œ ๊ณ„์† ํ‹€๋ ธ์—ˆ์Šต๋‹ˆ๋‹ค.
๋”ฐ๋ผ์„œ ์ด ๋ถ€๋ถ„๋งŒ ์œ ์˜ํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค.

์ฝ”๋“œ


import sys
from collections import deque
from functools import reduce


M, N, P = list(map(int, sys.stdin.readline().split()))
board = [list(sys.stdin.readline().strip()) for _ in range(M)]
powers = {}
player_indices = {}
for idx in range(P):
    player, power = list(sys.stdin.readline().split())
    powers[player] = int(power)
    player_indices[player] = idx
HP = int(input())

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


def pre_processing(board):
    positions = []

    for x in range(M):
        for y in range(N):
            if 'a' <= board[x][y] <= 'z':
                positions.append([x, y, board[x][y]])

    return positions


def bfs(positions):
    global HP

    q = deque()
    visit = [[[0] * P for _ in range(N)] for _ in range(M)]
    arrived = []

    for x, y, player in positions:
        q.append([x, y, player])
        visit[x][y][player_indices[player]] = 1

    while HP > 0:
        loop = len(q)

        for _ in range(loop):
            x, y, player = q.popleft()
            player_idx = player_indices[player]

            if board[x][y] == 'B':
                arrived.append(player)
                continue

            for i in range(4):
                nx, ny = x + dx[i], y + dy[i]

                if 0 <= nx < M and 0 <= ny < N:
                    if board[nx][ny] != 'X' and not visit[nx][ny][player_idx]:
                        q.append([nx, ny, player])
                        visit[nx][ny][player_idx] = 1

        # ๋ชฌ์Šคํ„ฐ ๊ณต๊ฒฉ
        if len(arrived):
            HP -= reduce(lambda acc, p: acc + powers[p], arrived, 0)

    return len(arrived)


def solution():
    positions = pre_processing(board)
    answer = bfs(positions)

    return answer


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

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

BOJ 14496 - ๊ทธ๋Œ€, ๊ทธ๋จธ๊ฐ€ ๋˜์–ด  (0) 2021.03.16
BOJ 9311 - Robot in a Maze  (0) 2021.03.14
BOJ 17521 - Byte Coin  (0) 2021.03.14
BOJ 20363 - ๋‹น๊ทผ ํ‚ค์šฐ๊ธฐ  (0) 2021.03.14
BOJ 14588 - Line Friends (Small)  (0) 2021.03.14

๐Ÿ’ฌ ๋Œ“๊ธ€