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

Algorithm/Baekjoon

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

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

 

๐ŸŸ  ๋ฌธ์ œ

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

 

15655๋ฒˆ: N๊ณผ M (6)

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

www.acmicpc.net

 

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

์ด ํ’€์ด๋Š” ๋˜,,, ๋ชจ๋“  ๊ฒƒ์„ N๊ณผ M(1)์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ’€์ด๊ฐ€ ์ด๋ฃจ์–ด์ง€๊ณ , ๊ทธ ๋‚ด๋ถ€์—์„œ ์ž…๋ ฅ๋งŒ ๋‹ฌ๋ผ์ง„๋‹ค. ๋Œ์•„๊ฐ€๋Š” ๋ฐฉ์‹์€ ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง€๊ณ  ํ˜„์žฌ ๋Œ์•„๊ฐ€๋Š” ์ˆ˜๋ณด๋‹ค ๋” ํฐ ์ˆ˜๊ฐ€ ์ˆ˜์—ด๋‚ด๋ถ€์—์„œ ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ +1 ํ•ด์ค€ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๋ฉด ๋œ๋‹ค. ์ž์„ธํ•œ ํ’€์ด๋Š” ์•„๋ž˜ ๋งํฌ๋ฅผ ๋ณด์ž..

์ „์ฒด์ ์ธ ์ž์„ธํ•œ ํ’€์ด๋ฅผ ๋ณด๊ณ ์‹ถ๋‹ค๋ฉด (1)์œผ๋กœ

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

 

์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ๋งŒ ์ด๋ฃจ์–ด์ง„ ์ˆ˜์—ด์„ ์ถ”๊ฐ€ํ•˜๋Š” ํ’€์ด๋ฅผ ๋ณด๊ณ ์‹ถ๋‹ค๋ฉด (2)

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

 

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

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

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