코딩테스트/코딩테스트 연습

[C++] 프로그래머스 단어 변환

Bradbury 2020. 10. 27. 18:11
728x90
#include <string>
#include <vector>
 
using namespace std;
 
int number = 9999999;
int N = 0;
int string_size = 0;
 
int string_check(string first, string second){
    int count = 0;
    for(int i=0; i<first.size(); i++){
        if(first[i]!=second[i])
            count++;
    }
    return count;
}
 
void recursive(string begin, string target, vector<string> words, vector<bool> check, int step){
    if(begin.compare(target)==0 && number > step){
        number = step;
        return;
    } 
    for(int i=0; i<N; i++){
        if(check[i] == true)
            continue;
        if(string_check(begin, words[i])==1){
            check[i] = true;
            recursive(words[i],target, words, check, step+1);
            check[i] = false;
        }
    }
    
}
 
int solution(string begin, string target, vector<string> words) {
    string_size = begin.size();
    N = words.size();
    vector<bool> check(N,false);
    recursive(begin, target, words, check, 0);
    if(number == 9999999)
        number = 0;
    return number;
}
728x90