코딩딩/BOJ

[백준 2310번 어드벤처 게임] 문제풀이

전낙타 2024. 8. 8. 15:42
import sys

sys.setrecursionlimit(10**6)
input = sys.stdin.readline


def dfs(cur, cost):
    global visited
    currentLocation = graph[cur]
    currentType = currentLocation[0]
    nextLocation = list(map(int, currentLocation[2:-1]))
    if currentType == "L":
        cost = max(cost, int(currentLocation[1]))
    elif currentType == "T":
        cost -= int(currentLocation[1])
        if cost < 0: return False
    if cur == n:
        return True
    visited.add(cur)
    for nextLo in nextLocation:
        if nextLo not in visited:
            if dfs(nextLo, cost):
                return True
    visited.remove(cur)
    return False


while True:
    n = int(input())
    if n == 0: break
    graph = [()]
    for i in range(n):
        graph.append((input().rstrip().split()))
    visited = set()
    if (dfs(1, 0)):
        print("Yes")
    else:
        print("No")

 

재귀도 다시 익혀야겠다  다까묵었넹