1일1알

백준 19949번 영재의 시험 C++ 본문

알고리즘

백준 19949번 영재의 시험 C++

영춘권의달인 2023. 4. 21. 12:50

https://www.acmicpc.net/problem/19949

 

19949번: 영재의 시험

컴퓨터공학과 학생인 영재는 이번 학기에 알고리즘 수업을 수강한다. 평소에 자신의 실력을 맹신한 영재는 시험 전날까지 공부를 하지 않았다. 당연하게도 문제를 하나도 풀지 못하였지만 다행

www.acmicpc.net

 

백트래킹

 

#include <iostream>
#include <string>
#include <vector>
#include <math.h>
#include <algorithm>
#include <utility>
#include <stack>
#include <queue>
#include <math.h>
#include <set>
#include <map>
#include <list>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>
#include <limits.h>

using namespace std;
using int64 = long long;

int ans = 0;

const int SIZE = 10;

vector<int> v(SIZE);
vector<int> v2;

void BT(int score) {
    if (v2.size() == SIZE) {
        if (score >= 5) ans++;
        return;
    }
    for (int i = 1; i <= 5; i++) {
        int nextScore = score;
        if (v2.size() <= 1) {
            v2.push_back(i);
            if (v2[v2.size() - 1] == v[v2.size() - 1]) nextScore++;
            BT(nextScore);
            v2.pop_back();
        }
        else {
            if (v2[v2.size() - 1] == i && v2[v2.size() - 2] == i) continue;
            v2.push_back(i);
            if (v2[v2.size() - 1] == v[v2.size() - 1]) nextScore++;
            BT(nextScore);
            v2.pop_back();
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    for (int i = 0; i < SIZE; i++) cin >> v[i];
    BT(0);
    cout << ans;
}