https://programmers.co.kr/learn/courses/30/lessons/43163#
#include <string>
#include <vector>
using namespace std;
bool check[51] = {false, };
int answer = 51;
void dfs(string begin, string target, vector<string> words, int depth){
for(int j=0;j<words.size();j++){
if(check[j] == false){
int same= 0;
for(int i=0;i<begin.size();i++){
if(begin[i] == words[j][i])
same ++;
}
if( same == begin.size()-1){
if(words[j] == target){
answer = min(answer, depth+1);
return;
}
check[j] = true;
dfs(words[j], target, words, depth+1);
check[j] = false;
}
}
}
}
int solution(string begin, string target, vector<string> words) {
dfs(begin, target, words, 0);
if(answer == 51)
answer = 0;
return answer;
}
#include <string>
#include <vector>
using namespace std;
string targ;
vector<string> word;
int size;
int wordsize;
int answer;
void bfs(int n, string begin, vector<int> visited){
if(begin.compare(targ)==0 && n < answer) answer = n;
for(int i=0; i<size; i++){
if(visited[i]==1) continue;
int t = 0;
for(int j=0; j<wordsize; j++){
if(begin[j] != word[i][j]) t++;
if(t>1) break;
}
if(t==1){
visited[i]=1;
bfs(n+1, word[i], visited);
}
}
}
int solution(string begin, string target, vector<string> words) {
answer = 999999999;
size = words.size();
wordsize = begin.length();
targ = target;
word = words;
vector<int> visited(size,0);
bfs(0, begin, visited);
if(answer == 999999999) return 0;
return answer;
}