본문 바로가기

백준 파이썬 코딩

백준 25919 Lost Edge 파이썬 최적화버전

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

 

25919번: Lost Edge

새로운 RPG게임인 Lost Edge에는 레이드 시스템이 존재한다. 하지만, 레이드에 참가하기 위해선 일정 레벨 이상을 찍고 레이드 장소에 모여야 한다. 플레이어는 상하좌우 4방향으로 한 칸씩만 움직

www.acmicpc.net

https://77dptjd.tistory.com/48

이전 포스팅을 하고나서 코드를 다시 보니 굳이 find함수를 안 만들어도 됐었다. 처음 레벨업 탐색중에 레이드 장소에 갈 수 있다면 레벨업이 끝나고도 다시 갈 수 있기에 처음 레벨업 탐색중에 레이드 장소에 도달했다는 것을 표기해주는 변수를 하나 추가하면 find함수 없이도 문제를 풀 수 있었다.

from collections import deque
import heapq
import sys
input = sys.stdin.readline

def bfs():
    global L,E,CH
    vist=[[False]*m for _ in range(n)]
    h=[]
    while q:
        x,y = q.popleft()
        for o in range(4):#4가지 방향 탐색
            nx=dx[o]+x
            ny=dy[o]+y
            if 0<=nx<n and 0<=ny<m and s[nx][ny]>=0 and not vist[nx][ny]:#방문 했던 곳 and 벽 and 레이드 장소 제외
                vist[nx][ny]=True#방문 표시
                heapq.heappush(h,(s[nx][ny],nx,ny))#heapq에 추가해줌 몬스터 레벨과 좌표
            elif 0<=nx<n and 0<=ny<m and s[nx][ny]==-2:#레이드 장소에 도달
                CH = 1#도달 할 수 있으므로 변수를 1로 변경
        while h and h[0][0]<L:#처치할 수 있는 몬스터가 있다.
            w,i,j=heapq.heappop(h)
            E += w#경험치 추가
            if E>=L:#레벨업 가능
            	E-=L
                L+=1#레벨업 해줌
            q.append((i,j))#몬스터를 처리했으니 갈 수 있는 길이 넓어짐
            s[i][j]= 0#더이상 경험치를 안 주니 0으로 설정

n,m = map(int,input().split())
L,E,K = map(int,input().split())
s = [list(map(int,input().split())) for _ in range(n)]
dx = [0,0,1,-1]
dy = [1,-1,0,0]
for i in range(n):
    for j in range(m):
        if s[i][j]==-3:
            s[i][j]=0
            q=deque()
            q.append((i,j))
        elif s[i][j]==-2:
            en = ((i,j))
CH=0#레이드 장소에 도달 가능한지 판단해주는 변수
bfs()
if L>=K and CH:#목표 레벨 달성 and 레이드 장소 갈 수 있음므로 O출력
    print("O")
else:#목표 레벨 미달성 or 레이드 장소에 못가니 X출력
    print("X")