Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- swift 점선
- tusit font 추가 방법
- swift dashed line
- reactorkit
- custom navigation bar
- Tuist
- DP
- button configuration
- identifiable
- rxdatasources
- domain data
- BFS
- 타임라인 포맷팅
- traits
- custom ui
- SWIFT
- custombottomsheet
- task cancel
- RxSwift
- claen architecture
- swift concurrency
- paragraph style
- swift custom ui
- UIKit
- swift 백준
- coordinator
- uikit toast
- task cancellation
- swift navigationcontroller
- swift bottomsheet
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 24480 알고리즘 수업 - 깊이 우선 탐색 2 본문
https://www.acmicpc.net/problem/24480
풀이 아이디어
일반적인 dfs입니다.
인접 정점을 내림차순으로 방문한다는 점을 주의해야하는 문제입니다
재귀로 풀이했습니다.
풀이
let nmr = readLine()!.split(separator: " ").map{Int($0)!}
let (n,m,r) = (nmr[0],nmr[1],nmr[2])
// []로 repeating하여 생성할 경우 반드시 타입을 선언해야합니다.
var graph: [[Int]] = Array(repeating: [], count: n + 1)
for i in 0..<m {
let input = readLine()!.split(separator: " ").map{Int($0)!}
let (a,b) = (input[0],input[1])
//무방향 그래프는 양방향 그래프를 뜻합니다.
graph[a].append(b)
graph[b].append(a)
}
var isVisited = Array(repeating: 0, count: n + 1)
var depth = 1
func dfs(node: Int) {
isVisited[node] = depth
// sorted를 활용해 내림차순으로 접근해줍니다
for next in graph[node].sorted(by: >) {
if isVisited[next] == 0 {
depth += 1
dfs(node: next)
}
}
}
dfs(node: r)
print(isVisited[1...].map{String($0)}.joined(separator: "\n"))
'Algorithm' 카테고리의 다른 글
[Swift 백준] 14248 점프 점프 (0) | 2025.03.06 |
---|---|
[Swift 백준] 16173 점프왕 쩰리 (Small) (0) | 2025.03.05 |
[Swift 백준] 7785 회사에 있는 사람 (0) | 2025.02.20 |
[Swift 백준] 5567 결혼식 (0) | 2025.02.19 |
[Swift 백준] 18352 특정 거리의 도시 찾기 (0) | 2025.02.17 |