728x90
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์ ๋ ๋ฐฑํธ๋ํน(Backtracking)์ ์ด์ฉํ์ฌ ํ์์ต๋๋ค. ๊ตฌํ ๊ณผ์ ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- ๊ด๋ฌผ์ 5๊ฐ์ฉ ์ฐจ๋ก๋๋ก ๋ถ๋ฆฌํ๊ณ , ๋จ์ ๊ด๋ฌผ ์๊ฐ 5๊ฐ๊ฐ ์ ๋ ๊ฒฝ์ฐ์๋ ๋จ์ ๊ด๋ฌผ์ ๋ชจ์กฐ๋ฆฌ ๋ด์ 2์ฐจ์ ๋ฐฐ์ด ์์ฑ
- ๊ณก๊ดญ์ด ๋ฐฐ์ด๊ณผ ๋ถ๋ฆฌํ 2์ฐจ์ ๊ด๋ฌผ ๋ฐฐ์ด์ ์ํํ๋ฉด์, ๊ณก๊ดญ์ด๋ฅผ ๋ค ์ผ๊ฑฐ๋ ๊ด๋ฌผ์ด ์์ ๋๊น์ง ํผ๋ก๋ ํฉ์ฐ ์งํ
- ํฉ์ฐํ ํผ๋ก๋๊ฐ ์ด์ ์ ๊ณ์ฐํ ์ต์๊ฐ ํผ๋ก๋๋ณด๋ค ์๋ค๋ฉด ๋ณ๊ฒฝ
- ์ดํ ํ ๋จ๊ณ ๋ค๋ก ๊ฐ์ ๋ค์ ํ์์ ์๋์ง ์งํ
๋ฌธ์ ๋ฅผ ํ๊ณ ๋์ ๋ค๋ฅธ ๋ถ๋ค ํ์ด๋ ์ด๋จ๊น ํ๊ณ ์ฐพ์๋ดค๋๋ฐ, ๊ทธ๋ฆฌ๋(Greedy)๋ก ํผ ์ฌ๋๋ค๋ ๊ณ์๋๊ตฐ์ ใทใท.. ์๊ฐ์ ์ข ๋ ํ์ฅํด ๋ด์ผ๊ฒ ์ต๋๋ค.
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <map>
#include <string>
#include <vector>
using namespace std;
const int MAX_CRAFTING = 5;
const int DIAMOND_PICK_IDX = 0;
const int IRON_PICK_IDX = 1;
const int STONE_PICK_IDX = 2;
map<pair<int, string>, int> stressFatigues {
{make_pair(DIAMOND_PICK_IDX, "diamond"), 1}, // ๋ค์ด์ ๊ณก๊ดญ์ด, ๋ค์ด์ ๊ด๋ฌผ
{make_pair(DIAMOND_PICK_IDX, "iron"), 1}, // ๋ค์ด์ ๊ณก๊ดญ์ด, ์ฒ ๊ด๋ฌผ
{make_pair(DIAMOND_PICK_IDX, "stone"), 1}, // ๋ค์ด์ ๊ณก๊ดญ์ด, ๋ ๊ด๋ฌผ
{make_pair(IRON_PICK_IDX, "diamond"), 5}, // ์ฒ ๊ณก๊ดญ์ด, ๋ค์ด์ ๊ด๋ฌผ
{make_pair(IRON_PICK_IDX, "iron"), 1}, // ์ฒ ๊ณก๊ดญ์ด, ์ฒ ๊ด๋ฌผ
{make_pair(IRON_PICK_IDX, "stone"), 1}, // ์ฒ ๊ณก๊ดญ์ด, ๋ ๊ด๋ฌผ
{make_pair(STONE_PICK_IDX, "diamond"), 25}, // ๋ ๊ณก๊ดญ์ด, ๋ค์ด์ ๊ด๋ฌผ
{make_pair(STONE_PICK_IDX, "iron"), 5}, // ๋ ๊ณก๊ดญ์ด, ์ฒ ๊ด๋ฌผ
{make_pair(STONE_PICK_IDX, "stone"), 1}, // ๋ ๊ณก๊ดญ์ด, ๋ ๊ด๋ฌผ
};
int CalculteStressFatigue(const int currentPick, const vector<string>& selectedMinerals) {
int result = 0;
for (int i = 0; i < selectedMinerals.size(); i++)
result += stressFatigues[make_pair(currentPick, selectedMinerals[i])];
return result;
}
void SearchMinStressFatigue(vector<int>& picks, const vector<vector<string>>& splitMinerals, int currentIdx, int& sum, int& minValue) {
bool isCompleteMining = true;
for (int i = 0; i < picks.size(); i++) {
if (picks[i] > 0 and currentIdx < splitMinerals.size()) {
isCompleteMining = false;
int totalStressFatigue = CalculteStressFatigue(i, splitMinerals[currentIdx]);
picks[i]--;
sum += totalStressFatigue;
SearchMinStressFatigue(picks, splitMinerals, currentIdx + 1, sum, minValue);
sum -= totalStressFatigue;
picks[i]++;
}
}
if (isCompleteMining) {
if (sum < minValue)
minValue = sum;
}
}
int FindMinStressFatigue(const vector<vector<string>>& splitMinerals, vector<int>& picks) {
int sum = 0;
int minValue = 2147483647;
SearchMinStressFatigue(picks, splitMinerals, 0, sum, minValue);
return minValue;
}
vector<vector<string>> SplitMinerals(const vector<string>& originalMinerals) {
vector<vector<string>> result;
for (int i = 0; i < originalMinerals.size(); i += MAX_CRAFTING) {
vector<string> minerals;
for (int j = i; j < originalMinerals.size() and j < i + MAX_CRAFTING; j++)
minerals.push_back(originalMinerals[j]);
result.push_back(minerals);
}
return result;
}
int solution(vector<int> picks, vector<string> minerals) {
vector<vector<string>> splitedMinerals = SplitMinerals(minerals);
int answer = FindMinStressFatigue(splitedMinerals, picks);
return answer;
}
728x90
๋ฐ์ํ
'๐คAlgorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. ๋กค์ผ์ดํฌ ์๋ฅด๊ธฐ | C++ (2) | 2023.06.08 |
---|---|
[Programmers] Lv2. ๋ฆฌ์ฝ์ณ ๋ก๋ด | C++ (0) | 2023.06.07 |
[Programmers] Lv2. ๋ง๋ฒ์ ์๋ฆฌ๋ฒ ์ดํฐ | C++ (0) | 2023.06.05 |
[Programmers] Lv2. ๋ค์ ์๋ ํฐ ์ ์ฐพ๊ธฐ | C++ (0) | 2023.06.04 |
[Programmers] Lv2. ๋ชจ์ ์ฌ์ | C++ (0) | 2023.06.04 |