링크 : https://school.programmers.co.kr/learn/courses/30/lessons/43163
#include <string>
#include <vector>
#include <iostream>
using namespace std;
string target_word;
vector<string> words_list;
bool check[51] = {false};
int answer = 100;
void dfs(string begin, int result) {
if (target_word == begin) {
answer = result;
return;
}
for (int i = 0; i < words_list.size(); i++) {
int count = 1;
for (int j = 0; j < words_list[i].size(); j++) {
if (begin[j] != words_list[i][j]) count++;
if (count == 3) break;
}
if (count == 2) {
if (check[i] == false) {
check[i] = true;
dfs(words_list[i], result + 1);
check[i] = false;
}
}
}
}
int solution(string begin, string target, vector<string> words) {
target_word = target;
words_list = words;
dfs(begin,0);
if (answer == 100) answer = 0;
return answer;
}