728x90
반응형
https://www.acmicpc.net/problem/18429
#include <iostream>
using namespace std;
int n, k;
int gain[8];
bool visited[8];
int weight = 500;
int answer = 0;
void dfs(int count){
if(count == n) answer++;
else{
for(int i = 0; i < n; i++){
if(!visited[i]){
visited[i] = true;
if(weight + gain[i] - k >= 500){
weight += gain[i] - k;
dfs(count + 1);
weight -= gain[i] - k;
}
visited[i] = false;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> k;
for(int i = 0; i < n; i++){
cin >> gain[i];
}
dfs(0);
cout << answer;
return 0;
}
이 문제를 next_permutation으로 풀 수 없는 이유
반례
3 4
4 4 4
정답: 6
출력: 1
next_permutation은 중복이 있는 원소의 경우 중복인 경우를 제외하고 순열을 만들어준다.
따라서 백트래킹으로 중복 순열을 구해서 해결했다.
728x90
반응형
'Algorithm > BAEKJOON' 카테고리의 다른 글
[BOJ] 15565번 귀여운 라이언 (C++) (0) | 2023.05.05 |
---|---|
[BOJ] 1068번 트리 (C++) (0) | 2023.03.19 |
[BOJ] 2263번 트리의 순회 (C++) (0) | 2023.03.18 |
[BOJ] 11725번 트리의 부모 찾기 (C++) (0) | 2023.03.17 |
[BOJ] 1761번 정점들의 거리 (C++) (0) | 2023.03.17 |