๋ฐ์ํ
๐ ๋ฌธ์
https://www.acmicpc.net/problem/15655
๐ ๋์ ํ์ด
์ด ํ์ด๋ ๋,,, ๋ชจ๋ ๊ฒ์ N๊ณผ M(1)์ ๊ธฐ๋ฐ์ผ๋ก ํ์ด๊ฐ ์ด๋ฃจ์ด์ง๊ณ , ๊ทธ ๋ด๋ถ์์ ์ ๋ ฅ๋ง ๋ฌ๋ผ์ง๋ค. ๋์๊ฐ๋ ๋ฐฉ์์ ์ค๋ฆ์ฐจ์์ผ๋ก๋ง ์ด๋ฃจ์ด์ง๊ณ ํ์ฌ ๋์๊ฐ๋ ์๋ณด๋ค ๋ ํฐ ์๊ฐ ์์ด๋ด๋ถ์์ ์ฌ ์ ์๊ธฐ ๋๋ฌธ์ ๋งค๊ฐ๋ณ์๋ก +1 ํด์ค ๊ฐ์ ๋๊ฒจ์ฃผ๋ฉด ๋๋ค. ์์ธํ ํ์ด๋ ์๋ ๋งํฌ๋ฅผ ๋ณด์..
์ ์ฒด์ ์ธ ์์ธํ ํ์ด๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด (1)์ผ๋ก
https://didu-story.tistory.com/371
์ค๋ฆ์ฐจ์์ผ๋ก๋ง ์ด๋ฃจ์ด์ง ์์ด์ ์ถ๊ฐํ๋ ํ์ด๋ฅผ ๋ณด๊ณ ์ถ๋ค๋ฉด (2)
https://didu-story.tistory.com/372
๐ ์ ๋ต์ฝ๋
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 = [Int]()
var answer = ""
private func dfs(start: Int) {
if stack.count == m {
answer += stack.map{ String($0) }.joined(separator: " ")
answer += "\n"
return
}
for i in start..<n {
if !stack.contains(arr[i]) {
stack.append(arr[i])
dfs(start: i+1)
stack.removeLast()
}
}
}
dfs(start: 0)
print(answer)
๋ฐ์ํ