[algorithm]프로그래머스 네트워크

855 단어 algorithmalgorithm

https://programmers.co.kr/learn/courses/30/lessons/43162

function solution(n, computers) {
    let count =0
    let visited=new Array(computers.length).fill(false)
    for(let i =0;i<computers.length;i++){
        if(visited[i]===false){
            count++
            bfs(computers,i,visited)
        }
    }
    return count;
}

const bfs=(computers,nodeIndex,visited)=>{
   let queue=[]
   queue.push(computers[nodeIndex])
    while(queue.length!==0){
        console.log(queue.length)
        let curr=queue.shift()
    for(let j in curr){
        if(nodeIndex!==j&&curr[j]==1&&visited[j]===false){
            visited[j]=true
            queue.push(computers[j])
        }
    }
 
    }
}

좋은 웹페이지 즐겨찾기