[백준 11403] 경로 찾기 with node.js

waterglasses·2021년 9월 26일
0

📌 문제링크

https://www.acmicpc.net/problem/11403

📌 풀이

  • 플로이드–와샬 알고리즘을 이용한다.
  • 지나는 점(k)를 기준으로 i에서 k를 갈 수 있고 k에서 i를 갈 수 있으면 지나는 점이 된다

📌 코드

const fs = require('fs');
const stdin = (
  process.platform === 'linux'
    ? fs.readFileSync('/dev/stdin').toString().trim()
    : `7
0 0 0 1 0 0 0
0 0 0 0 0 0 1
0 0 0 0 0 0 0
0 0 0 0 1 1 0
1 0 0 0 0 0 0
0 0 0 0 0 0 1
0 0 1 0 0 0 0`
).split('\n');

const input = (() => {
  let line = 0;
  return () => stdin[line++];
})();

const N = parseInt(input());
const graph = Array.from(new Array(N), () => input().split(' ').map(Number));

for (let k = 0; k < N; k++) {
  for (let i = 0; i < N; i++) {
    for (let j = 0; j < N; j++) {
      if (graph[i][j]) continue;
      if (graph[i][k] && graph[k][j]) graph[i][j] = 1;
    }
  }
}

console.log(graph.map((vertices) => vertices.join(' ')).join('\n'));
profile
매 순간 성장하는 개발자가 되려고 노력하고 있습니다.

0개의 댓글