https://programmers.co.kr/learn/courses/30/lessons/43163
function solution(begin, target, words) {
var answer = 0;
var q = [];
if (words.indexOf(target) < 0) return 0;
var cnt = 0;
q.push([begin, cnt]);
while (q.length !== 0) {
var word = q.shift();
cnt = word[1];
if (word[0] === target) {
answer = word[1];
break;
}
for (var w of words) {
var diff = 0;
for (var i = 0; i < w.length; i++) {
if (w[i] === word[0][i]) continue;
else diff++;
}
if (diff === 1) {
q.push([w, cnt + 1]);
}
}
}
return answer;
}
bfs 사용
'algorithm > dfs,bfs' 카테고리의 다른 글
[백준/2667] 단지번호붙이기 (0) | 2020.04.06 |
---|---|
[백준/2606] 바이러스 (0) | 2020.04.04 |
[백준/1260] DFS와 BFS (0) | 2020.04.04 |
댓글