https://programmers.co.kr/learn/courses/30/lessons/43163
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <iostream>
using namespace std;
int solution(string begin, string target, vector<string> words) {
int answer = 2e9;
int visited[55];
for(int i=0; i<55; i++){
visited[i] = 2e9;
}
if(find(words.begin(),words.end(),target) == words.end()) return 0;
queue<pair<string,int>> q;
q.push(make_pair(begin,0));
while(!q.empty()){
string str = q.front().first;
int count = q.front().second;
q.pop();
if(str == target){
answer = min(answer,count);
continue;
}
for(int i=0; i<words.size(); i++){
int diff = 0;
for(int j=0; j<words[i].size(); j++){
if(str[j] != words[i][j]) diff++;
}
if(diff == 1){
if(visited[i] > count+1){
q.push(make_pair(words[i],count+1));
visited[i] = count+1;
}
}
}
}
return answer;
}
'프로그래머스' 카테고리의 다른 글
아이템 줍기 C++ (0) | 2022.02.07 |
---|---|
2 X n 타일링 C++ (0) | 2022.01.30 |
경주로 건설 C++ (0) | 2022.01.29 |
네트워크 C++ (0) | 2022.01.23 |
N으로 표현 C++ (0) | 2022.01.23 |