import Foundation
func solution(_ n:Int, _ computers:[[Int]]) -> Int {
var answer: Int = 0
var visited: [Bool] = Array(repeating: false, count: n)
func dfs(_ vertex: Int) {
visited[vertex] = true
for index in 0..<n {
if computers[vertex][index] == 1 && visited[index] == false {
dfs(index)
}
}
}
for index in 0..<n {
if !visited[index] {
answer += 1
dfs(index)
}
}
return answer
}
'프로그래머스 > Lv.3' 카테고리의 다른 글
[Swift] 베스트앨범 (0) | 2023.06.18 |
---|---|
[Swift] 기지국 설치 (0) | 2023.06.18 |
[Swift] 숫자 게임 (0) | 2023.06.18 |
[Swift] 여행경로 (0) | 2023.04.20 |
[Swift] 단어 변환 (0) | 2023.04.18 |