From 4f20884858d5a17e1fe428f390e6515c7267121f Mon Sep 17 00:00:00 2001 From: ljedd2 Date: Sat, 3 Aug 2024 23:36:55 +0900 Subject: [PATCH 1/2] =?UTF-8?q?2024-08-03=20=ED=94=BC=EB=A1=9C=EB=8F=84.py?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\355\224\274\353\241\234\353\217\204..py" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "LJEDD2/2024-2/\354\231\204\354\240\204\355\203\220\354\203\211/\355\224\274\353\241\234\353\217\204..py" diff --git "a/LJEDD2/2024-2/\354\231\204\354\240\204\355\203\220\354\203\211/\355\224\274\353\241\234\353\217\204..py" "b/LJEDD2/2024-2/\354\231\204\354\240\204\355\203\220\354\203\211/\355\224\274\353\241\234\353\217\204..py" new file mode 100644 index 00000000..da273b4f --- /dev/null +++ "b/LJEDD2/2024-2/\354\231\204\354\240\204\355\203\220\354\203\211/\355\224\274\353\241\234\353\217\204..py" @@ -0,0 +1,49 @@ +# 첫 번째 코드 +def solution(k, dungeons): + + def DFS(k, cnt): + nonlocal answer + + answer = max(answer, cnt) + for i in range(len(dungeons)): + # 던전 = ["최소 필요 피로도", "소모 피로도"] + min_fatigue, use_fatigue = dungeons[i][0], dungeons[i][1] + + # 현재 피로도가 해당 던전을 방문하기 위한 최소 피로도보다 클 때 + if not visited[i] and k >= min_fatigue: + visited[i] = True + + # 백트래킹 : 이전 노드로 다시 back할 때, + # 해당 노드를 방문하기 전의 피로도로 다시 복구 + DFS(k-use_fatigue, cnt+1) + visited[i] = False + + answer = 0 + visited = [False] * len(dungeons) # visited + DFS(k, 0) + + return answer + + +# 개선된 코드 +def solution(k, dungeons): + def dfs(k, cnt): + # 함수 내부에서 값을 갱신할 수 있는 비전역 변수로 선언 + nonlocal answer + + answer = max(answer, cnt) + + # enumerate()를 활용하여 각 던전의 인덱스와 값을 동시에 반복 + for i, (min_fatigue, use_fatigue) in enumerate(dungeons): + + if not visited[i] and k >= min_fatigue: + + visited[i] = True + dfs(k - use_fatigue, cnt + 1) + visited[i] = False + + answer = 0 + visited = [False] * len(dungeons) + dfs(k, 0) + + return answer \ No newline at end of file From 9ed98a6b1279a852bcebef699f396b44802fe5cf Mon Sep 17 00:00:00 2001 From: ljedd2 Date: Sat, 3 Aug 2024 23:38:23 +0900 Subject: [PATCH 2/2] 2024-08-03 --- LJEDD2/2024-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/LJEDD2/2024-2/README.md b/LJEDD2/2024-2/README.md index 2feff67d..1dd7f96b 100644 --- a/LJEDD2/2024-2/README.md +++ b/LJEDD2/2024-2/README.md @@ -7,3 +7,4 @@ | 3차시 | 2024.07.24 | 깊이/너비 우선 탐색(DFS/BFS) | 여행경로 | [#3](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/131) | | 4차시 | 2024.07.27 | 그래프 | 가장 먼 노드 | [#4](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/133) | | 5차시 | 2024.07.31 | 다이나믹프로그래밍 | 정수 삼각형 | [#5](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/139) | +| 6차시 | 2024.08.03 | 완전탐색 | 피로도 | [#6](https://github.com/AlgoLeadMe/AlgoLeadMe-4/pull/141) |