알고리즘/BOJ

백준 12869번 뮤탈리스크

꾸준함. 2024. 3. 18. 02:40

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

 

12869번: 뮤탈리스크

1, 3, 2 순서대로 공격을 하면, 남은 체력은 (12-9, 10-1, 4-3) = (3, 9, 1)이다. 2, 1, 3 순서대로 공격을 하면, 남은 체력은 (0, 0, 0)이다.

www.acmicpc.net

 

주어진 N과 SCV의 최대 체력이 크지 않기 때문에 브루트포스로 풀어도 되는 문제였습니다.

visited set의 키를 {scv 체력의 순열, 몇 번째 공격}으로 두는 것이 핵심이었습니다.

억지로 푼 느낌...

 

#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
using namespace std;
const int INF = 987654321;
const int MAX = 3;
int result = INF;
vector<int> scvs(MAX);
set<pair<vector<int>, int>> visited;
bool allDestroyed(vector<int> &scvs)
{
for (int i = 0; i < MAX; i++)
{
if (scvs[i])
{
return false;
}
}
return true;
}
void func(int cnt, vector<int> scvs)
{
if (allDestroyed(scvs))
{
result = min(result, cnt);
return;
}
vector<int> multalisk;
multalisk.push_back(1);
multalisk.push_back(3);
multalisk.push_back(9);
visited.insert({ scvs, cnt });
do
{
vector<int> copyScvs(MAX);
for (int i = 0; i < MAX; i++)
{
copyScvs[i] = scvs[i];
scvs[i] = max(scvs[i] - multalisk[i], 0);
}
if (visited.find({ scvs, cnt + 1 }) == visited.end())
{
func(cnt + 1, scvs);
}
for (int i = 0; i < MAX; i++)
{
scvs[i] = copyScvs[i];
}
} while (next_permutation(multalisk.begin(), multalisk.end()));
}
int main(void)
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
for (int i = 0; i < N; i++)
{
cin >> scvs[i];
}
func(0, scvs);
cout << result << "\n";
return 0;
}
view raw .cpp hosted with ❤ by GitHub

 

개발환경:Visual Studio 2022

 

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

반응형

'알고리즘 > BOJ' 카테고리의 다른 글

백준 14497번 주난의 난(難)  (0) 2024.03.23
백준 17071번 숨바꼭질 5  (0) 2024.03.20
백준 17298번 오큰수  (0) 2024.03.15
백준 2870번 수학숙제  (1) 2024.03.13
백준 2910번 빈도 정렬  (0) 2024.03.13