๐Ÿ“ Coding Test Study/Algorithm Problem

[C++][Programmers][์™„์ „ํƒ์ƒ‰] ํ”ผ๋กœ๋„

ibelieveinme 2023. 4. 26. 01:32
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/87946

 

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

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

programmers.co.kr

 

[ํ•ด๊ฒฐ์ฑ…]

1) ๋˜์ „ ๊ฐœ์ˆ˜๊ฐ€ ์ตœ๋Œ€ 8๊ฐœ๋ผ์„œ permutation์œผ๋กœ ์ˆœ์„œ ์กฐํ•ฉ ๋ฝ‘๊ณ  ํƒ์ƒ‰ํ•˜๋ฉฐ ์ตœ๋Œ€ ํƒํ—˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๋œ๋‹ค.

2) permutation๋ง๊ณ ๋„ DFS๋กœ๋„ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

[์•Œ์•„๋‘˜ ๊ฒƒ]

* DFS/BFS Algorithm ๋ฐ ๊ตฌํ˜„๋ฒ•

 

[Algorithm][C++] BFS/DFS

< BFS(Breath First Search): ๋„ˆ๋น„ ์šฐ์„  ํƒ์ƒ‰ > Queue์™€ ๋ฐฉ๋ฌธ๋…ธ๋“œ๋ฅผ ์ฒดํฌํ•  ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์ฝ”๋”ฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. (Queue๋Š” ์„ ์ž…์„ ์ถœ ๊ตฌ์กฐ๋ฅผ ๊ฐ–๋Š” ์ž๋ฃŒ๊ตฌ์กฐ๋กœ, ๋จผ์ € ๋„ฃ์€ ๊ฐ’์„ ๋จผ์ € ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋„“๊ฒŒ

i-believe-in-me.tistory.com

 

* C#์˜ foreach๋ฌธ ๊ฐ™์€ for๋ฌธ ์‚ฌ์šฉ๋ฒ•

vector<int> v = {1,2,3};

for(const auto& vValue: v){
	cout << vValue; // 1, 2, 3
}

 

[์ฝ”๋“œ]

#include <string>
#include <vector>
#include <algorithm>
using namespace std;

int solution(int k, vector<vector<int>> dungeons) {
    int answer = -1;

    //1. ์กฐํ•ฉ ๋Œ๋ฆฌ๊ธฐ ์‰ฝ๊ฒŒ ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ
    sort(dungeons.begin(), dungeons.end());
    
    //2. ์กฐํ•ฉ ์ˆœ์„œ๋Œ€๋กœ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ตœ๋Œ€ ๋˜์ „ ํƒ์ƒ‰ ๊ฐ€๋Šฅ ์ˆ˜ ๊ตฌํ•˜๊ธฐ.
    do{
        int count = 0;   
        int currentEnergy = k;
        for(const auto& dungeon: dungeons){
            int minEnergy = dungeon[0];
            int useEnergy = dungeon[1];
            if(minEnergy <= currentEnergy){
                currentEnergy -= useEnergy;
                count++;
            }
            else break;
        }
        answer = max(count, answer);
    }while(next_permutation(dungeons.begin(), dungeons.end()));
        
    return answer;
}
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

bool isVisited[8] = {false, };
int answer;

void DFS(int currentEnergy, int count, vector<vector<int>> dungeons){
    //ํ˜„์žฌ Energy๊ฐ€ 0 ์ดํ•˜๋ฉด ๋ฐ”๋กœ returnํ•ด์„œ ์‹œ๊ฐ„์ค„์ด๊ธฐ
    if(currentEnergy <= 0){
        return;
    }
    for(int i = 0; i < dungeons.size(); i++){
        int minEnergy = dungeons[i][0];
        int useEnergy = dungeons[i][1];
        if(!isVisited[i] && currentEnergy >= minEnergy){
            //ํ•ด๋‹น ๊ฐ€์ง€์—์„  ๋”์ด์ƒ ํƒ์ƒ‰์„ ์•ˆํ•˜๊ธฐ ์œ„ํ•ด ๋ฐฉ๋ฌธ true ์ฒ˜๋ฆฌ
            isVisited[i] = true;
            //๋‹ค์Œ ๊นŠ์ด๋กœ DFS ํƒ์ƒ‰
            DFS(currentEnergy-useEnergy, count+1, dungeons);
            //๋ฆฌํ„ด์‹œ ์žฌํƒ์ƒ‰์„ ์œ„ํ•ด ๋ฐฉ๋ฌธ false์ฒ˜๋ฆฌ
            isVisited[i] = false;
        }
    }
    answer = max(answer, count);
}

int solution(int currentEnergy, vector<vector<int>> dungeons) {    
    //ํ˜„์žฌ ์—๋„ˆ์ง€ ๊ฐ’๊ณผ ํƒ์ƒ‰ ๊นŠ์ด ๊ฐ’, dungeons ๋ฒกํ„ฐ๋ฅผ ๋„˜๊น€ 
    DFS(currentEnergy, 0, dungeons);
    return answer;
}
728x90