링크 : https://school.programmers.co.kr/learn/courses/30/lessons/43163
#include <string>
#include <vector>
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
bool visited[51];
int solution(string begin, string target, vector<string> words) {
int answer = 0;
memset(visited, false, sizeof(visited));
queue<pair<string, int>> q;
q.push({begin, 0});
while(!q.empty()){
string s = q.front().first;
int cnt = q.front().second;
q.pop();
cout << s << "\n";
if(s == target) {
return cnt;
}
else{
for(int i = 0; i < words.size(); i++){
int diff = 0;
for(int j = 0; j < words[0].size(); j++)
if(words[i][j] != s[j]) diff++;
if(diff == 1 && !visited[i]){
visited[i] = true;
q.push({words[i], cnt + 1});
}
}
}
}
return 0;
}