Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 15657๋ฒˆ - N๊ณผ M(8) (feat. ๋“œ๋””์–ด๋งˆ์ง€๋ง‰... DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

๊ฐ์ž ๐Ÿฅ” 2023. 1. 14. 13:43
๋ฐ˜์‘ํ˜•

๐ŸŸ  ๋ฌธ์ œ

https://www.acmicpc.net/problem/15657

 

15657๋ฒˆ: N๊ณผ M (8)

N๊ฐœ์˜ ์ž์—ฐ์ˆ˜์™€ ์ž์—ฐ์ˆ˜ M์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ์•„๋ž˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ๊ธธ์ด๊ฐ€ M์ธ ์ˆ˜์—ด์„ ๋ชจ๋‘ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. N๊ฐœ์˜ ์ž์—ฐ์ˆ˜๋Š” ๋ชจ๋‘ ๋‹ค๋ฅธ ์ˆ˜์ด๋‹ค. N๊ฐœ์˜ ์ž์—ฐ์ˆ˜ ์ค‘์—์„œ M๊ฐœ๋ฅผ ๊ณ ๋ฅธ ์ˆ˜์—ด

www.acmicpc.net

 

๐ŸŸ  ๋‚˜์˜ ํ’€์ด

์ค‘๋ณต์„ ํ—ˆ์šฉํ•ด์ค˜์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— if !stack.contains ๋ถ€๋ถ„์„ ๋นผ์„œ ๋ฐฉ๋ฌธํ™•์ธ์„ ์•ˆํ•ด์ฃผ์—ˆ๊ณ , ํ•ด๋‹น ์ˆ˜๋ณด๋‹ค๋Š” ์ž‘๊ฑฐ๋‚˜ ํฐ ์ˆ˜๊ฐ€ ์˜†์— ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ํ˜„์žฌ ๋ฐฉ๋ฌธํ•˜๊ณ  ์žˆ๋Š” i ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ์—ˆ๋‹ค. 

๋˜‘๊ฐ™์€ ๋ฌธ์ œ๋Š” N๊ณผ M(4)๋ฒˆ!

https://didu-story.tistory.com/375

 

[๋ฐฑ์ค€] (Swift) 15651๋ฒˆ - N๊ณผ M(4) (feat. DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

๐ŸŸ  ๋ฌธ์ œ https://www.acmicpc.net/problem/15652 15652๋ฒˆ: N๊ณผ M (4) ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด

didu-story.tistory.com

 

๐ŸŸ  ์ •๋‹ต์ฝ”๋“œ

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)
๋ฐ˜์‘ํ˜•