티스토리 뷰
dfs로 풀어보려했으나.. 잘 모르겠음. 나는 역시 아직도 dfs 쪼랩이다.
그래서 일단 풀긴 풀어야하니 굉장히 극극극 비효율로 풀었다. 완전탐색...
from itertools import permutations
def solution(k, dungeons):
answer = []
orders = list(permutations([i for i in range(0, len(dungeons))]))
for order in orders:
score = k
count = 0
for i in order:
if score >= int(dungeons[i][0]):
score = score - int(dungeons[i][1])
count = count + 1
answer.append(count)
return max(answer)
흠...
내가 dfs를 쓰지 못했던 이유는 backtracking을 생각하지 못했기 때문이다.
이미 방문했던 원소를 visited[i] = 1로 두면 다시 뒤 돌아 나와야할 때는 어떻게 할지 (이게 백트래킹인 것도 몰랐음)에 대한 아이디어가 없었기 때문이다.
다른사람의 풀이에 달린 댓글을 보고 이게 백트래킹이라는 것을 알았다.
answer = 0
N = 0
visited = []
def dfs(k, cnt, dungeons):
global answer
if cnt > answer:
answer = cnt
for j in range(N):
if k >= dungeons[j][0] and not visited[j]:
visited[j] = 1
dfs(k - dungeons[j][1], cnt + 1, dungeons)
visited[j] = 0
def solution(k, dungeons):
global N, visited
N = len(dungeons)
visited = [0] * N
dfs(k, 0, dungeons)
return answer
나는 이렇게 풀고 싶었다.
흠.. 내일은 백준에서 백트래킹 문제들(8개)를 쭉 풀어보면서 익혀봐야겠다.
'코딩테스트 대비' 카테고리의 다른 글
| [백준] 15650번 N과M(2) (0) | 2022.02.24 |
|---|---|
| [백준] 15649번 N과M(1) (0) | 2022.02.24 |
| [프로그래머스] 수식 최대화 (0) | 2022.02.23 |
| [프로그래머스] 뉴스 클러스터링 (0) | 2022.02.23 |
| [프로그래머스] 단어 변환 (BFS) (0) | 2022.02.22 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- torch
- notfound
- torchscript
- 최소신장트리
- LGSVL
- shellscript
- pytorch
- CUDA
- 카카오
- 설치하기
- 백트래킹
- 백준
- 코딩테스트
- BFS
- version
- 다익스트라
- 이것이코딩테스트다
- n과m
- numpy
- docker
- dfs
- error
- Python
- PIP
- matplotlib
- tensorflow
- 설치
- 프로그래머스
- 동적프로그래밍
- 파이썬
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함
