๋ณธ๋ฌธ์œผ๋กœ ๋ฐ”๋กœ๊ฐ€๊ธฐ
728x90
๐Ÿ”—๋ฌธ์ œ ๋ณด๋Ÿฌ๊ฐ€๊ธฐ
 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

๐Ÿ‘จ‍๐Ÿ’ปํ’€์ด ๊ณผ์ •

 

์ €๋Š” ๋ฐฑํŠธ๋ž˜ํ‚น(Backtracking)์„ ์ด์šฉํ•˜์—ฌ ํ’€์—ˆ์Šต๋‹ˆ๋‹ค. ๊ตฌํ˜„ ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

  1. ๊ด‘๋ฌผ์„ 5๊ฐœ์”ฉ ์ฐจ๋ก€๋Œ€๋กœ ๋ถ„๋ฆฌํ•˜๊ณ , ๋‚จ์€ ๊ด‘๋ฌผ ์ˆ˜๊ฐ€ 5๊ฐœ๊ฐ€ ์•ˆ ๋  ๊ฒฝ์šฐ์—๋Š” ๋‚จ์€ ๊ด‘๋ฌผ์„ ๋ชจ์กฐ๋ฆฌ ๋‹ด์•„ 2์ฐจ์› ๋ฐฐ์—ด ์ƒ์„ฑ
  2. ๊ณก๊ดญ์ด ๋ฐฐ์—ด๊ณผ ๋ถ„๋ฆฌํ•œ 2์ฐจ์› ๊ด‘๋ฌผ ๋ฐฐ์—ด์„ ์ˆœํšŒํ•˜๋ฉด์„œ, ๊ณก๊ดญ์ด๋ฅผ ๋‹ค ์ผ๊ฑฐ๋‚˜ ๊ด‘๋ฌผ์ด ์—†์„ ๋•Œ๊นŒ์ง€ ํ”ผ๋กœ๋„ ํ•ฉ์‚ฐ ์ง„ํ–‰
  3. ํ•ฉ์‚ฐํ•œ ํ”ผ๋กœ๋„๊ฐ€ ์ด์ „์— ๊ณ„์‚ฐํ•œ ์ตœ์†Ÿ๊ฐ’ ํ”ผ๋กœ๋„๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ๋ณ€๊ฒฝ
  4. ์ดํ›„ ํ•œ ๋‹จ๊ณ„ ๋’ค๋กœ ๊ฐ€์„œ ๋‹ค์Œ ํƒ์ƒ‰์€ ์—†๋Š”์ง€ ์ง„ํ–‰

 

๋ฌธ์ œ๋ฅผ ํ’€๊ณ  ๋‚˜์„œ ๋‹ค๋ฅธ ๋ถ„๋“ค ํ’€์ด๋Š” ์–ด๋–จ๊นŒ ํ•˜๊ณ  ์ฐพ์•„๋ดค๋Š”๋ฐ, ๊ทธ๋ฆฌ๋””(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
๋ฐ˜์‘ํ˜•