알고리즘/백준 문제풀이

[boj] 백준 20005 보스몬스터 전리품 python 풀이

감자156 2023. 9. 16. 17:10
반응형

문제

https://www.acmicpc.net/problem/20005

 

20005번: 보스몬스터 전리품

입력의 첫째 줄에는 멤멤월드의 지도의 크기를 나타내는 두 정수 M(6 ≤ M ≤ 1000), N(6 ≤ N ≤ 1000)과 플레이어의 수 P(1 ≤ P ≤ 26)가 주어진다. M은 지도의 세로 길이, N은 지도의 가로 길이이다. 입

www.acmicpc.net

 

문제 풀이

맵이 작아서 그냥 바로 bfs로 같은 거리에 있는 player의 dps 묶고, 매 depth 마다 보스 때려 hp -= accm_dps

 

코드

 import sys
input = sys.stdin.readline
import collections

## 입력
M, N, P = map(int,input().split())
MAP = []
for i in range(M):
    tmp = list(input().strip())
    for j in range(N):
        if tmp[j] == 'B':
            bx, by = i, j
            break
    MAP.append(tmp)

player_status = dict()
for _ in range(P):
    name, dps = input().split()
    player_status[name] = int(dps)
HP = int(input().strip())

'''
보스로부터 bfs 돌면서 같은 distance에 있는 플레이어들은 accm_dps로 붂어서 
한 루프 돌 때마다 패 -> HP < 0 되면 종료
'''
def bfs(MAP, bx, by, HP):
    accm_dps, accm_person = 0, 0
    q = collections.deque()
    q.append((bx,by))
    MAP[bx][by] = '0' # visited

    leng = len(q)
    while q and HP>0:
        cx, cy = q.popleft()

        for tx, ty in {(-1,0), (0,1), (1,0), (0,-1)}:
            nx = tx + cx
            ny = ty + cy

            if 0<=nx<M and 0<=ny<N and MAP[nx][ny] != '0' and MAP[nx][ny] != 'X':
                if 'a' <= MAP[nx][ny] <= 'z':
                    accm_person += 1
                    accm_dps += player_status[MAP[nx][ny]]
                q.append((nx,ny))
                MAP[nx][ny] = '0'

        leng -= 1
        if leng == 0:
            leng = len(q)
            HP -= accm_dps

    return accm_person

print(bfs(MAP, bx, by, HP))
반응형