Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 혼자 공부하는 C언어
- Stack
- JSON
- list 컬렉션
- s
- 윤성우 열혈자료구조
- 메모리구조
- 이것이 자바다
- Graph
- buffer
- 이스케이프 문자
- insertion sort
- C programming
- 알기쉬운 알고리즘
- coding test
- Serialization
- stream
- C 언어 코딩 도장
- Algorithm
- 윤성우의 열혈 자료구조
- R
- datastructure
- Selection Sorting
Archives
- Today
- Total
Engineering Note
61. 특정 수 만들기(DFS : MS 인터뷰) 본문
Problem Solving/Olympiad in Informatics
61. 특정 수 만들기(DFS : MS 인터뷰)
Software Engineer Kim 2021. 4. 22. 16:47it 취업을 위한 알고리즘 문제풀이
문제
코드
//61. 특정 수 만들기(DFS : MS 인터뷰)
#include <stdio.h>
int n, m, cnt;
int num[10];
void dfs(int depth, int total) {
if (depth == n) {
if (total == m)
++cnt;
return;
}
dfs(depth + 1, total + num[depth]);
dfs(depth + 1, total - num[depth]);
dfs(depth + 1, 0 + total);
}
int main() {
//freopen("input.txt", "rt", stdin);
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i) {
scanf("%d", &num[i]);
}
dfs(0, 0);
if (cnt == 0) {
printf("%d", -1);
}
else
printf("%d", cnt);
return 0;
}
문제해결방법
- DFS로 문제를 해결했다.
- depth별로 문제에 주어진 인덱스에 해당하는 요소를 +요소,-요소,0(해당 요소값 포함하지 않음 의미)으로 합을 구해 depth가 n이 될때 까지깊이우선 탐색을 진행하고 합이 m이 될때 카운트 값을 증가시키도록 코드를 구현했다.
'Problem Solving > Olympiad in Informatics' 카테고리의 다른 글
65. 미로탐색(DFS) (0) | 2021.05.04 |
---|---|
64. 경로 탐색(DFS) (0) | 2021.04.28 |
60. 합이 같은 부분집합(DFS : 아마존 인터뷰) (0) | 2021.04.15 |
60. 합이 같은 부분집합(DFS : 아마존 인터뷰) (0) | 2021.04.15 |
59. 부분집합(DFS) (0) | 2021.04.14 |
Comments