#include <string> #include <vector> #include <queue> #include <algorithm> using namespace std; bool check[51]; int solution(string begin, string target, vector<string> words) { int answer = 0; fill(check, check + 51, false); queue<pair<string, int>> mainQ; mainQ.push( { begin, 0 }); while (!mainQ.empty()) { string word = mainQ.front().first; int count = mainQ.front().second; mainQ.pop(); if(!word.compare(target)) { answer = count; break; } else if(count > words.size()) { break; } for(int idx = 0; idx < words.size(); idx++) { if(check[idx]) { continue; } int flag = -1; for(int i = 0; i < words[idx].size(); i++) { if(word[i] != words[idx][i]) { flag++; } if(i == words[idx].size() - 1 && !flag) { mainQ.push( { words[idx], count + 1 }); check[idx] = true; } } } } return answer; } | cs |
'problem solving' 카테고리의 다른 글
[프로그래머스]라면공장 (0) | 2020.03.13 |
---|---|
[프로그래머스]여행경로 (0) | 2020.03.13 |
[프로그래머스]네트워크 (0) | 2020.03.12 |
[프로그래머스]카펫 (0) | 2020.03.12 |
[프로그래머스]베스트앨범 (0) | 2020.03.12 |