https://programmers.co.kr/learn/courses/30/lessons/92342
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int N = 0;
int max_value = -1;
vector<int> answer;
void solve(int index, vector<int> info, int count, vector<int> ans, int score_ap, int score_lian) {
if (count == N) {
int sctmp = score_ap;
for (int k = index; k <= 10; k++) {
ans.push_back(0);
if (info[k] > 0) {
sctmp += (10 - k);
}
}
if (score_lian > sctmp) {
if (max_value < (score_lian-sctmp)) {
max_value = (score_lian - sctmp);
answer = ans;
}
else if (max_value == (score_lian - sctmp)) {
for (int i = 10; i >= 0; i--) {
if (ans[i] > answer[i]) {
max_value = score_lian - sctmp;
answer = ans;
break;
}
else if (ans[i] < answer[i]) {
break;
}
}
}
}
return;
}
else if (index == 11 && count < N) {
ans[10] = N - count;
if (score_lian > score_ap) {
if (max_value < (score_lian - score_ap)) {
max_value = score_lian - score_ap;
answer = ans;
}
else if (max_value == (score_lian - score_ap)) {
for (int i = 10; i >= 0; i--) {
if (ans[i] > answer[i]) {
max_value = score_lian - score_ap;
answer = ans;
break;
}
else if (ans[i] < answer[i]) {
break;
}
}
}
}
return;
}
if (count > N) return;
//과녁 한개 더 맞춤
int num = info[index];
if (N >= num + 1) {
ans.push_back(num + 1);
solve(index + 1, info, count + num + 1, ans, score_ap, score_lian + (10 - index));
ans.pop_back();
}
//안맞춤
ans.push_back(0);
if (info[index] >0) solve(index + 1, info, count, ans, score_ap + (10 - index), score_lian);
else solve(index + 1, info, count, ans, score_ap, score_lian);
ans.pop_back();
}
vector<int> solution(int n, vector<int> info) {
N = n;
solve(0, info, 0, {}, 0, 0);
if (max_value == -1) answer.push_back(-1);
return answer;
}
'프로그래머스' 카테고리의 다른 글
신고 결과 받기 C++ (0) | 2022.02.19 |
---|---|
k진수에서 소수 개수 구하기 C++ (0) | 2022.02.19 |
주차 요금 계산 C++ (0) | 2022.02.18 |
파괴되지 않은 건물 C++ (0) | 2022.02.15 |
스타 수열 C++ (0) | 2022.02.11 |