알고리즘/백준(BOJ)

백준 - 2309 일곱 난쟁이

시나모온 2020. 7. 29. 14:49

문제 링크입니다 : https://www.acmicpc.net/problem/2309

 

2309번: 일곱 난쟁이

아홉 개의 줄에 걸쳐 난쟁이들의 키가 주어진다. 주어지는 키는 100을 넘지 않는 자연수이며, 아홉 난쟁이의 키는 모두 다르며, 가능한 정답이 여러 가지인 경우에는 아무거나 출력한다.

www.acmicpc.net

 

 

브루트 포스

 

 

 

 

 

#include <iostream>
#include <vector>
#include <algorithm>
#define DWARF_NUM 9

using namespace std;

vector<int> dwarfs(DWARF_NUM);
vector<int> choosed;


bool search(int index, int sum) {
    if(index == DWARF_NUM) {
        if(choosed.size() == 7 && sum == 100) {
            sort(choosed.begin(), choosed.end());
            for(int i = 0; i < choosed.size(); i++) {
                cout << choosed[i] << '\n';
            }
            return true;
        }
        return false;
    }

    choosed.push_back(dwarfs[index]);
    if(search(index + 1, sum + dwarfs[index])) return true;
    choosed.pop_back();
    if(search(index + 1, sum)) return true;
    return false;
}


int main() {

    for(int i = 0; i < DWARF_NUM; i++) {
        cin >> dwarfs[i];
    }

    search(0, 0);

    return 0;
}

 

 

 

개발 환경 : vscode

지적, 조언, 질문 환영입니다! 댓글 남겨주세요~