이번에 풀어본 문제는
프로그래머스 피로도 입니다.
class Solution {
var answer = 0
fun solution(k: Int, dungeons: Array<IntArray>): Int {
dfs(dungeons, k, 0, Array(dungeons.size){false})
return answer
}
fun dfs(map: Array<IntArray>, tired: Int, count: Int, isVisited: Array<Boolean>) {
if (count > answer)answer = count
for ((idx, value) in map.withIndex()) {
if (!isVisited[idx] && value[0] <= tired) {
isVisited[idx] = true
dfs(map, tired - value[1], count + 1, isVisited)
isVisited[idx] = false
}
}
}
}
보유한 피로도가 충분하다면 입장할 수 있는 던전이 있고, 해당 던전에서 피로도를 소모합니다.
이와 같은 조건으로 주어진 던전을 순회할 때, 최대로 많은 던전을 입장하는 개수를 반환하는 문제입니다.
최대 입력되는 던전의 개수가 8개여서 조금 투박하게 풀었는데, 다른 방법이 딱히 떠오르진 않았던 것 같네요 ㅋㅋㅋㅋ 있는 그대로 dfs로 구현해보았습니다.