250x250
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
- NIO
- string
- deque
- dfs
- priority_queue
- set
- math
- 리소스모니터링
- Union-find
- GC로그수집
- javascript
- BFS
- 스프링부트
- sql
- 스택
- spring boot
- 힙덤프
- Calendar
- 큐
- Java
- List
- date
- CSS
- JPA
- html
- map
- alter
- scanner
- Properties
- union_find
Archives
- Today
- Total
매일 조금씩
프로그래머스 코테 연습 : 타겟 넘버 [C++] DFS 본문
728x90
반응형
DFS의 정석과 같은 문제다.
스택이 아닌 재귀로 문제를 해결했다.

이문제의 경우 각각의 경우에 따른 sum과 count를 알아야해서 sum과 count가 전역으로 선언되어서는 안된다.
dfs 가 호출될때 sum, count를 같이 넘겨 주는 식으로 해야한다 각각의 dfs 경로별로 sum과 count 가 다르기 때문이다.
#include <string>
#include <vector>
using namespace std;
int answer = 0;
void dfs(vector<int> numbers, int target, int sum, int count){
if(count == numbers.size()){
if(sum == target){
answer++;
}
return;
}
// 합하고 다음걸로 넘어감
dfs(numbers, target, sum + numbers[count], count+1);
dfs(numbers, target, sum - numbers[count], count+1);
}
int solution(vector<int> numbers, int target) {
// dfs를 호출할때마다 sum과 count를 알아야 하므로 파라미터에 포함시킨다.
dfs(numbers, target, 0, 0);
return answer;
}
728x90
반응형
'알고리즘 > Graph (DFS, BFS)' 카테고리의 다른 글
| 프로그래머스 코테 연습 : 단어 변환 [C++] DFS (0) | 2021.04.19 |
|---|---|
| 프로그래머스 코테 연습 : 네트워크 [C++] DFS/BFS (0) | 2021.04.18 |
| 백준 2583번 : 영역 구하기 [C++] (0) | 2020.08.09 |
| 백준 2468번 : 안전 영역 [C++] (0) | 2020.08.09 |
| 백준 6603번 : 로또 [C++] (0) | 2020.08.09 |