문제 링크입니다: https://www.acmicpc.net/problem/16198
16198번: 에너지 모으기
N개의 에너지 구슬이 일렬로 놓여져 있고, 에너지 구슬을 이용해서 에너지를 모으려고 한다. i번째 에너지 구슬의 무게는 Wi이고, 에너지를 모으는 방법은 다음과 같으며, 반복해서 사용할 수 있다. 에너지 구슬 하나를 고른다. 고른 에너지 구슬의 번호를 x라고 한다. 단, 첫 번째와 마지막 에너지 구슬은 고를 수 없다. x번째 에너지 구슬을 제거한다. Wx-1 × Wx+1의 에너지를 모을 수 있다. N을 1 감소시키고, 에너지 구슬을 1번부터 N번까지로 다
www.acmicpc.net
N이 최대 10이기 때문에 완전탐색법으로 쉽게 구현할 수 있는 문제였습니다.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <vector> | |
#include <algorithm> | |
using namespace std; | |
int func(vector<int> marbles) | |
{ | |
if (marbles.size() == 2) | |
{ | |
return 0; | |
} | |
int result = 0; | |
for (int i = 1; i < marbles.size() - 1; i++) | |
{ | |
int temp = marbles[i - 1] * marbles[i + 1]; | |
vector<int> tempMarbles; | |
for (int j = 0; j < marbles.size(); j++) | |
{ | |
if (j == i) | |
{ | |
continue; | |
} | |
tempMarbles.push_back(marbles[j]); | |
} | |
temp += func(tempMarbles); | |
result = max(result, temp); | |
} | |
return result; | |
} | |
int main(void) | |
{ | |
ios_base::sync_with_stdio(0); | |
cin.tie(0); | |
int N; | |
cin >> N; | |
vector<int> marbles(N); | |
for (int i = 0; i < N; i++) | |
{ | |
cin >> marbles[i]; | |
} | |
cout << func(marbles) << "\n"; | |
return 0; | |
} |


개발환경:Visual Studio 2017
지적, 조언, 질문 환영입니다! 댓글 남겨주세요~
반응형
'알고리즘 > BOJ' 카테고리의 다른 글
백준 10993번 별 찍기 - 18 (0) | 2020.04.07 |
---|---|
백준 10899번 King of penalty (0) | 2020.04.07 |
백준 2980번 도로와 신호등 (2) | 2020.04.04 |
백준 4796번 캠핑 (0) | 2020.03.28 |
백준 15355번 Programiranje (4) | 2020.03.23 |