30 lines
927 B
Swift
30 lines
927 B
Swift
func dfs(_ n:Int, _ m:Int, _ visited:inout [Bool], _ result:inout [Int], _ nums:[Int]) {
|
|
if result.count == m {
|
|
for n in result {
|
|
print(n, terminator: " ")
|
|
}
|
|
print()
|
|
return
|
|
}
|
|
|
|
for next in 0..<n {
|
|
if !visited[next] {
|
|
visited[next] = true
|
|
result.append(nums[next])
|
|
dfs(n,m,&visited,&result,nums)
|
|
_ = result.popLast()
|
|
visited[next] = false
|
|
}
|
|
}
|
|
}
|
|
|
|
if let input1 = readLine(), let NM:[Int] = input1.split(separator: " ").compactMap({Int($0)}) as? [Int], NM.count == 2 {
|
|
let N:Int = NM[0]
|
|
let M:Int = NM[1]
|
|
|
|
if let input2 = readLine(), let nums:[Int] = input2.split(separator: " ").compactMap({Int($0)}) as? [Int], nums.count == N {
|
|
var visited:[Bool] = Array(repeating: false, count: N)
|
|
var result:[Int] = []
|
|
dfs(N, M, &visited, &result, nums.sorted())
|
|
}
|
|
} |