반응형
넓이우선탐색으로 풀었다
//visited bfs
function compare(s1, s2){
let tmp=0;
for(let i=0; i<s1.length; i++){
if(s1[i]===s2[i]) tmp++;
}
if(tmp===s1.length-1) return true;
else return false;
}
function solution(begin, target, words) {
var answer = 0;
let visited = new Array(words.length).fill(false);
let queue=[];
queue.push([begin,0]);
while(queue.length!==0){
let now = queue.shift();
for(let i=0; i<words.length; i++){
if(visited[i]) continue;
if(compare(now[0],words[i])){
if(words[i]===target) return now[1]+1;
visited[i] = true;
queue.push([words[i],now[1]+1]);
}
}
}
return answer;
}
반응형
'코테' 카테고리의 다른 글
[Programmers] 징검다리 건너기 JS (0) | 2022.01.21 |
---|---|
[Programmers] 이중우선순위큐 JS (0) | 2022.01.21 |
[Programmers] 표편집 JS (0) | 2022.01.21 |
[Programmers] N으로 표현 JS (0) | 2022.01.20 |
[Programmers] N개의 최소공배수 JS (0) | 2022.01.20 |