프로그래머스 모음사전 C++ dfs

2023. 9. 17. 21:48알고리즘

#include <string>
#include <vector>
#include <iostream>

using namespace std;

char alpha[5] = {'A', 'E', 'I', 'O', 'U'};
int answer, cnt;

void dfs(string s, string word){
    if (s==word){
        answer=cnt;
        return;        
    }
    if (s.length()>=5){
        return;
    }
    for (int i=0;i<5;i++){
        cnt++;
        dfs(s+alpha[i], word);
    }
}

int solution(string word) {
    dfs("", word);
    return answer;
}

해결코드이다.

하나씩 dfs로 순회해주면서 풀어주면된당