νΌλ‘λ lv2
πλ¬Έμ λ§ν¬
1
2
3
<νμ΄λ°©λ² 2κ°>
1. λ°±νΈλνΉ
2. μμ΄ & μ°μ°
1
2
3
4
k : νμ¬ νΌλ‘λ
count : λμ μ
dungeon : λμ μ 보
visitted : λ°©λ¬Έμ¬λΆ
1. λ°±νΈλνΉ
DFS β> μ¬κΈ°μλ νλ²μ λμ μ κ° μ μλ λκΉμ§ νμν ν μ΄μ λ¨κ³λ‘ λμκ°λ μμ
info ν¨μλ₯Ό λ¨Όμ μ μ, λ°μμ solution ν¨μκ° info ν¨μλ₯Ό νΈμΆν΄ κ²°κ³Όλ₯Ό λ°ν
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
max_count = 0
def dfs(k, count, dungeons, visited):
global max_count
if count > max_count:
max_count = count
for i in range(len(dungeons)):
if visited[i] != 1 and k >= dungeons[i][0]:
visited[i] = True
dfs(k - dungeons[i][1], count + 1, dungeons, visited)
visited[i] = False
def solution(k, dungeons):
global max_count
visited = [False] * len(dungeons)
dfs(k, 0, dungeons, visited)
return max_count
max_count = 0
def dfs(k, count, dungeons, visited):
global max_count
λμ λ μ μλ μ΅λ νμ κ° μ΄κΈ°νλ‘ 0μΌλ‘ μ€μ
μλ dfs ν¨μ λ°κΉ₯μ μλ max_countλ₯Ό μ μλ³μμΈ globalλ‘
global max_countλ‘ μΈκΈν΄μ£Όλ©΄μ μμμλ μ μ© κ°λ₯
if count > max_count:
max_count = count
μ°λ¦¬κ° μ°Ύλ κ²μ μ΅λ λμ λ μ μλ νμ
if visited[i] != 1 and k >= dungeons[i][0]:
visited[i] = True
λ§μ½ λμ μ λ°©λ¬Έ μνκ³ , νμ¬ νΌλ‘λ >= λμ μ λ°λ©΄μ μλͺ¨λ νΌλ‘λ
μ΄ κ²½μ°μ
ν΄λΉ λμ μ λ°©λ¬Ένλ€κ³ νμ¬ True
dfs(k - dungeons[i][1], count + 1, dungeons, visited) visited[i] = False
- μ¬κ·μ μΌλ‘ νλ² ν¨μ νΈμΆ β> dfs(~~)
ν΄λΉ λμ μ λ°©λ¬Έν μνμμ λ¨μ체λ ₯κ³Ό λ°©λ¬Έν λμ μλ₯Ό κ°μ§κ³ μ¬κ·μ μΌλ‘ λ€μλμ νμ
visited =0 ββ λ°±νΈλνΉμ μνν΄ ν΄λΉλμ μ λ°©λ¬Ένμ§ μμ μνλ‘ λμκ°
def solution(k, dungeons): global max_count
μ΄μ λμ μ΅λ λ μ μλ νμ ꡬν©μλ€!
visited = [False] * len(dungeons)
visited λΌλ 리μ€νΈλ₯Ό λ§λ€λ, 리μ€νΈμ κ° μμλ₯Ό False
νΉμ 0
μΌλ‘ μ΄κΈ°ν
dfs(k, 0, dungeons, visited) return max_count
0 νΉμ False ββ μ΄λ€ λμ λ λμ§ μμ μνμμ μΈκ² μ΅λλ€
dfs μκ³ λ¦¬μ¦μμ λμ λ°©λ¬Έμ¬λΆ μΆμ ν ν, μ΅λ λ μ μλ νμ λ°ν
2. μμ΄ & μ°μ°
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from itertools import permutations
def solution(k, dungeons):
max_count = 0
dungeon_permut = list(permutations(dungeons, len(dungeons)))
for dungeons_path in dungeon_permut:
current_hp = k
count = 0
for required_minimum_hp, consume in dungeons_path:
if required_minimum_hp <= current_hp:
count += 1
current_hp -= consume
elif current_hp < required_minimum_hp:
break
if max_count < count:
max_count = count
return max_count