- 출처가 올림피아드라고 해서 긴장했는데 다행히 맞추었다, 유후~!
- 1원짜리 동전을 0개 쓰는 경우, 1개 쓰는 경우, 2개 쓰는 경우 / 10원짜리 동전을 0개 쓰는 경우, 1개 쓰는 경우... 5개 쓰는 경우 .. 이런식으로 각 동전 타입에 대해 몇 개를 쓸 것인지 가짓수를 뻗어나가면 된다는 것이다
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
|
def DFS(level, sum):
global cnt
if sum > target:
return
if level == types:
if sum == target:
cnt+=1
else:
for i in range(0, nums[level]+1):
DFS(level+1, sum + (coins[level]*i))
if __name__ == '__main__':
target = int(input())
types = int(input())
coins = []
nums = []
cnt = 0
for _ in range(types):
coin, num = map(int, input().split())
coins.append(coin)
nums.append(num)
DFS(0, 0)
print(cnt)
|
cs |
728x90
'Data Structures & Algorithms' 카테고리의 다른 글
[Algorithms] 깊이/넓이 우선 탐색 (DFS, BFS) 활용-송아지 찾기(BFS:상태트리 탐색) (0) | 2022.05.29 |
---|---|
[Algorithms] 깊이/넓이 우선 탐색 (DFS, BFS) 활용-알파코드(DFS) (0) | 2022.05.28 |
[Algorithms] 깊이/넓이 우선 탐색 (DFS, BFS) 활용-양팔저울(DFS) (0) | 2022.05.28 |
[Algorithms] 완전탐색(백트랙킹, 상태트리와 CUT EDGE)/깊이우선탐색(DFS) Basic-경로 탐색(그래프 DFS) (0) | 2022.05.27 |
[Algorithms] 완전탐색(백트랙킹, 상태트리와 CUT EDGE)/깊이우선탐색(DFS) Basic-인접 행렬(가중치 방향 그래프) (0) | 2022.05.27 |