Skip to content

Commit 14e357f

Browse files
committed
add: 백준/경로찾기(11403)
1 parent 029935e commit 14e357f

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

File renamed without changes.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs');
2+
const stdin = (process.platform === 'linux'
3+
? fs.readFileSync('/dev/stdin').toString().trim()
4+
: `3
5+
0 1 0
6+
0 0 1
7+
1 0 0
8+
`
9+
).split('\n');
10+
11+
const input = (() => {
12+
let line = 0;
13+
return () => stdin[line++];
14+
})();
15+
16+
const N = parseInt(input());
17+
18+
const answer = [];
19+
20+
for (let row = 0; row < N; row++) {
21+
const col = input()
22+
.split(' ')
23+
.map((each) => parseInt(each));
24+
25+
answer.push(col);
26+
}
27+
28+
for (let k = 0; k < N; k++) {
29+
for (let row = 0; row < N; row++) {
30+
for (let col = 0; col < N; col++) {
31+
if (answer[row][k] == 1 && answer[k][col] == 1) {
32+
answer[row][col] = 1;
33+
}
34+
}
35+
}
36+
}
37+
38+
answer.forEach((e) => console.log(e.join(' ')));
39+
40+
/*
41+
42+
## How
43+
Floyed Warshall 알고리즘을 사용하면 쉽게 풀 수 있다. 이외에도 DFS와 BFS를 활용해 풀 수 있다.
44+
45+
한 정점에서 다른 정점까지의 최단거리는 다익스트라나 벨만 포드 알고리즘을 사용한다. 모든 정점에서 모든 정점으로의 최단거리를 구할 때 Floyed Warshall 알고리리즘을 사용할 수 있다.
46+
47+
문제에서는 i에서 j로 갈 수 있는지 여부를 판별하라고 요구한다. 여기서 Floyed Warshall을 사용하여 i에서 k로, k에서 j로 가는 것이 가능한지 여부를 판별하면 된다.
48+
49+
해당 조건이 부합하는 경우 해당 경로에 1을 대입하여 문제를 해결할 수 있다.
50+
51+
## Retrospective
52+
53+
*/

0 commit comments

Comments
 (0)