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 | 31 |
Tags
- swift associated type
- swift 점선
- uikit toast
- button configuration
- swift opaque type
- reactorkit
- DP
- custom ui
- swift custom ui
- traits
- custombottomsheet
- swift concurrency
- 버튼 피드백
- UIKit
- swift 백준
- domain data
- swift dashed line
- scene delegate
- BFS
- task cancel
- swift bottomsheet
- task cancellation
- identifiable
- coordinator
- Tuist
- SWIFT
- swift navigationcontroller
- RxSwift
- custom navigation bar
- swift existential type
Archives
- Today
- Total
김경록의 앱 개발 여정
[Swift 백준] 15650 N과 M (2) 본문
풀이 아이디어
- 백트래킹을 이용하는 문제입니다.
- resultArr의 길이에 조건을 두어 불필요한 탐색을 방지합니다.
- 한 경로를 탐색한 후 removeLast()를 호출하여 상태를 복구하고 다른 경로를 탐색합니다.
풀이
import Foundation
let input = readLine()!.split(separator: " ").map{ Int($0)! }
let n = input[0]
let m = input[1]
var resultArr: [Int] = []
func dfs(_ start: Int) {
if resultArr.count == m {
print(resultArr.map { String($0) }.joined(separator: " "))
return
}
// outOFIndex방지
if start > n { return }
for i in start...n {
resultArr.append(i)
dfs(i + 1)
resultArr.removeLast()
}
}
dfs(1)
'Algorithm' 카테고리의 다른 글
[Swift 백준] 1965 상자넣기 (0) | 2025.01.13 |
---|---|
[Swift 백준] 13305 주유소 (0) | 2025.01.09 |
[Swift 백준] 15649 N과 M (1) (0) | 2025.01.08 |
[Swift 백준] 11497 통나무 건너뛰기 (0) | 2025.01.08 |
[Swift 백준] 15922 아우으 우아으이야!! (0) | 2025.01.08 |