Baekjoon [BaekJoon] 2644 번 촌수계산 문제 - (nodejs) - 728x90 문제 번호 : 2644 번문제 바로가기 ☞ https://www.acmicpc.net/problem/2644 <<< 문제 내용 >>> const fs = require("fs"); const filePath = process.platform === "linux" ? "/dev/stdin" : "./input.txt"; let input = fs.readFileSync(filePath).toString().trim().split("\n"); const N = Number(input.shift()); let graph = Array.from(Array(N + 1), () => new Array(0)); const [a, b] = input[0].split(" ").map(Number); const graphLen = Number(input[1]); // 그래프 연결 for (let i = 2; i < 2 + graphLen; i++) { const [start, end] = input[i].split(" ").map(Number); graph[start].push(end); graph[end].push(start); } // bfs 시작 const bfs = (idx, cnt) => { let queue = [[idx, cnt]]; let visited = new Array(N + 1).fill(false); while (queue.length) { [idx, cnt] = queue.shift(); visited[idx] = true; while (graph[idx].length) { let temp = graph[idx].pop(); if (temp === b) return cnt + 1; queue.push([temp, cnt + 1]); } } return -1; }; console.log(bfs(a, 0)); 도움이 되셨다면 공감 부탁드립니다. 공유하기 URL 복사카카오톡 공유페이스북 공유엑스 공유 게시글 관리 구독하기MoveRoad's Factory 저작자표시 Contents 당신이 좋아할만한 콘텐츠 [Programmers] 다리를 지나가는 트럭 문제 - (javascript) 2022.02.12 [BaekJoon] 16932 번 모양 만들기 문제 - (nodejs) 2022.02.10 [BaekJoon] 2206 번 벽 부수고 이동하기 문제 - (nodejs) 2022.02.07 [BaekJoon] 3190 번 뱀 문제 - (nodejs) 2022.02.05 댓글 0 + 이전 댓글 더보기