๋ฐ์ํ
๐ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/11403
๐ ๋ฌธ์ ํ์ด
start์ง์ ์ ๊ธฐ์ค์ผ๋ก dfs๋ฅผ ๋๋ ค์ค ๊ฒ์ด๋ค.
dfs๋ ๋๊ฐ์ง์ ํ๋ผ๋ฏธํฐ๋ฅผ ์ ๋ฌ๋ฐ์ ๊ฑด๋ฐ, ์๋ถ๋ถ์ (์์ ํ), ๋๋ฒ์งธ ํ๋ผ๋ฏธํฐ๋ (ํ์ฌ ๋ณด๊ณ ์๋ํ)์ผ๋ก ๊ธฐ์ตํ๋ฉด๋๋ค. ๊ทธ๋์ ์ด๊ฒ์ ๋๋ ค์ฃผ๋ฉด, ์๋์ฒ๋ผ ๋์๊ฐ๊ฒ ๋๋ค.
๐ ์ ๋ต ์ฝ๋
import Foundation
let n = Int(readLine()!)!
var graph = [[Int]]()
for i in 0..<n {
graph.append(readLine()!.split(separator: " ").map { Int(String($0))! })
}
var visited = Array(repeating: false, count: n)
var result = Array(repeating: Array(repeating: 0, count: n), count: n)
func dfs(start: Int, now: Int) {
for i in 0..<n {
if graph[now][i] == 1 && !visited[i] {
result[start][i] = 1
visited[i] = true
dfs(start: start, now: i)
}
}
}
for i in 0..<n {
dfs(start: i, now: i)
visited = Array(repeating: false, count: n)
}
for j in result {
for r in j {
print(r, terminator: " ")
}
print()
}
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 17427๋ฒ - ์ฝ์์ ํฉ2 (0) | 2022.12.23 |
---|---|
[๋ฐฑ์ค] (Swift) 1037๋ฒ - ์ฝ์ (0) | 2022.12.23 |
[๋ฐฑ์ค] (Swift) 1654๋ฒ - ๋์ ์๋ฅด๊ธฐ (feat. ์ด๋ถํ์) (0) | 2022.09.04 |
[๋ฐฑ์ค] (Swift) 1931๋ฒ - ํ์์ค ๋ฐฐ์ (์ค๋ฒ1) (feat. ๊ทธ๋ฆฌ๋, ํ์๋ฒ) (0) | 2022.08.31 |
[๋ฐฑ์ค] (Swift) 1679๋ฒ - ์ซ์๋์ด (dp) (0) | 2022.07.28 |