๋ฐ์ํ
๐ ๋ฌธ์
https://www.acmicpc.net/problem/15657
๐ ๋์ ํ์ด
์ค๋ณต์ ํ์ฉํด์ค์ผํ๊ธฐ ๋๋ฌธ์ if !stack.contains ๋ถ๋ถ์ ๋นผ์ ๋ฐฉ๋ฌธํ์ธ์ ์ํด์ฃผ์๊ณ , ํด๋น ์๋ณด๋ค๋ ์๊ฑฐ๋ ํฐ ์๊ฐ ์์ ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ๋งค๊ฐ๋ณ์๋ก ํ์ฌ ๋ฐฉ๋ฌธํ๊ณ ์๋ i ๊ฐ์ ๋๊ฒจ์ฃผ์๋ค.
๋๊ฐ์ ๋ฌธ์ ๋ N๊ณผ M(4)๋ฒ!
https://didu-story.tistory.com/375
๐ ์ ๋ต์ฝ๋
let nm = readLine()!.split(separator: " ").map{ Int($0)! }
let n = nm[0]
let m = nm[1]
let arr = readLine()!.split(separator: " ").map{ Int($0)! }.sorted(by: <)
var stack = [String]()
var answer = ""
private func dfs(start: Int) {
if stack.count == m {
answer += stack.joined(separator: " ") + "\n"
return
}
for i in start..<n {
stack.append(String(arr[i]))
dfs(start: i)
stack.removeLast()
}
}
dfs(start: 0)
print(answer)
๋ฐ์ํ