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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 15654๋ฒˆ - N๊ณผ M(5) (๋‘๋ฒˆ์งธ ํ’€์ด) (feat. DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

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

๐ŸŸ  ๋ฌธ์ œ

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

 

15654๋ฒˆ: N๊ณผ M (5)

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

www.acmicpc.net

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

N๊ณผ M (1)๋ฒˆ๋ฌธ์ œ์™€ ์•„์˜ˆ ๋˜‘๊ฐ™์€๋ฌธ์ œ์ด๋‹ค. ๊ทธ๋ƒฅ ์ž…๋ ฅ๋ฐ›์€ ์ˆ˜๋กœ ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•˜๋Š” ๊ฒƒ์ผ๋ฟ!! ํ•ด๋‹น ํฌ์ŠคํŒ…์€ ์•„๋ž˜ ๋‚จ๊ฒจ ๋†“๊ฒ ๋‹ค. ์ €๊ธฐ์— ๋” ์ž์„ธํ•œ ํ’€์ด๊ฐ€ ๋‚˜์™€์žˆ๋‹ค.

์ด๊ฑฐ ๊ทธ๋ƒฅ ๋ฌธ์ œ๋„, ์ฝ”๋“œ๋„ ์™ธ์šฐ๊ฒŸ๋‹ค ;; ์–ผ๋ฅธ๋๋‚ด์•ผ์ง€ N๊ณผ M์‹œ๋ฆฌ์ฆˆ,,,, ์ด๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•˜๋ฉด์„œ ์žฌ๊ท€๋ฅผ ํ’€๋‹ค๋ณด๋‹ˆ DFS ์žฌ๊ท€๋กœ ๋Œ๋ฆฌ๋Š” ๋ฐฉ์‹์€ ์™„์ „ํžˆ ์ดํ•ดํ•  ๊ฒƒ ๊ฐ™๋‹ค! ํ™”์ดํŒ…

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

 

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

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

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 = [Int]()
var answer = ""

private func dfs() {
    if stack.count == m {
        answer += stack.map{ String($0) }.joined(separator: " ")
        answer += "\n"
        return
    }

    for i in 0..<n {
        if !stack.contains(arr[i]) {
            stack.append(arr[i])
            dfs()
            stack.removeLast()
        }
    }
}

dfs()
print(answer)
๋ฐ˜์‘ํ˜•