ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/11403
package main
import (
"bufio"
"fmt"
"os"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var graph = make([][]int, n)
for i := 0; i < n; i++ {
graph[i] = make([]int, n)
}
for i := 0; i < n; i++ {
for j := 0; j < n; j++ {
fmt.Fscanf(reader, "%d ", &graph[i][j])
}
}
result := floydWarshall(graph)
for i := 0; i < len(result); i++ {
for j := 0; j < len(result[i]); j++ {
fmt.Fprintf(writer, "%d ", result[i][j])
}
fmt.Fprintln(writer, "")
}
}
func floydWarshall(graph [][]int) [][]int {
for i := 0; i < len(graph); i++ { // j->i->k ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋ ๊ฒฝ์ฐ j->k ๊ฒฝ๋ก๊ฐ ์กด์ฌํ๋ ๊ฒ์ผ๋ก ๊ฐฑ์ ํ๋ค.
for j := 0; j < len(graph); j++ {
for k := 0; k < len(graph); k++ {
if graph[j][i] != 0 && graph[i][k] != 0 {
graph[j][k] = 1
}
}
}
}
return graph
}
๐จ Python3
# https://www.acmicpc.net/problem/11403
import sys
def floyd_warshall(graph):
for i in range(len(graph)):
for j in range(len(graph)):
for k in range(len(graph)):
if graph[j][i] != 0 and graph[i][k] != 0:
graph[j][k] = 1
return graph
if __name__ == "__main__":
n = int(sys.stdin.readline())
graph = []
for i in range(n):
graph.append(list(map(int, sys.stdin.readline().split())))
result = floyd_warshall(graph)
for i in range(n):
for j in range(n):
print("{} ".format(result[i][j]), end='')
print()
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 18243๋ฒ / Small World Network [Go][Python3] (0) | 2021.01.06 |
---|---|
BOJ / 11265๋ฒ / ๋๋์ง ์๋ ํํฐ [Go][Python3] (0) | 2021.01.05 |
BOJ / 2178๋ฒ / ๋ฏธ๋ก ํ์ [Go][Python3] (0) | 2021.01.03 |
BOJ / 9372๋ฒ / ์๊ทผ์ด์ ์ฌํ [Go][Python3] (0) | 2021.01.02 |
BOJ / 2740๋ฒ / ํ๋ ฌ ๊ณฑ์ [Go][Python3] (0) | 2021.01.01 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ์๊ฐ๊ต์ฒด
- go
- ๋ธ๋ฃจํธํฌ์ค
- ๋งฅ๋ถ
- ํ๋ก์ด๋์์ฌ
- ๋ฐ๋
- Macbook pro 2012 mid 13
- ์คํ
- ์ด๋ถํ์
- ์๋ฐ
- ๋ถํ ์ ๋ณต
- BFS
- ํ
- ๋งฅ๋ถํ๋ก
- BOJ
- ballet
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- dp
- Algorithm
- java
- Golang
- ๋ฐฑ์ค
- python3
- dfs
- baekjoon
- ์๊ณ ๋ฆฌ์ฆ
- AWS
- MongoDB
- ๋ชฝ๊ณ ๋๋น
- ํด์๋งต
- Total
- Today
- Yesterday