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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 15656๋ฒˆ - N๊ณผ M(7) (feat. ๋นก์น˜๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ, DFS, ๋ฐฑํŠธ๋ž˜ํ‚น)

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

๐ŸŸ  ๋ฌธ์ œ

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

 

15656๋ฒˆ: N๊ณผ M (7)

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

www.acmicpc.net

 

๐ŸŸ  ์ฒซ๋ฒˆ์งธ ํ’€์ด - ์‹œ๊ฐ„์ดˆ๊ณผ (ใ…‚ใ„ทใ…‚ใ„ท)

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

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

 

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

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

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 {
        stack.append(arr[i])
        dfs()
        stack.removeLast()
    }
}

dfs()
print(answer)

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด๋Š” ์ด๊ฒƒ๊ณผ ๊ฐ™๋‹ค. ์•ž์„œ print๋ฅผ ์—ฌ๋Ÿฌ๋ฒˆ ํ•ด์ฃผ๋Š” ๊ฒƒ์ด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, ์• ์ดˆ์— answer์„ string ํƒ€์ž…์œผ๋กœ ๋งŒ๋“ค์–ด์ฃผ๊ณ , ๋งˆ์ง€๋ง‰์— ๋”ฑ ํ•œ๋ฒˆ๋งŒ ์ถœ๋ ฅํ•ด์ฃผ์—ˆ๋‹ค. ๊ทผ๋ฐ... 

์™œ.. ์™œ.... ๋ฐฑ์ค€์—์„œ ์ฃผ์–ด์ง„ ์‹œ๊ฐ„๋„ 1์ดˆ์˜€๊ธฐ ๋•Œ๋ฌธ์— ์‹œ๊ฐ„๋„ ์ ๋‹นํ•˜๋‹ค๊ณ  ์ƒ๊ฐ๋๋‹ค. ๋„์ €ํžˆ ๋ชจ๋ฅด๊ฒ ์–ด์„œ ์ธํ„ฐ๋„ท์„ ์ฐพ์•„๋ดค๋‹ค.

https://jwonylee.tistory.com/entry/15656

 

BOJ: #15656 - N๊ณผ M (7)

06:47 ์ด ๋ฌธ์ œ๋Š” ์›๋ž˜ toPick == 0์ผ ๋•Œ picked๋ฅผ ๋ฐ”๋กœ ์ถœ๋ ฅํ•˜๊ฒŒ ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ์ œ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚œ๋‹ค. ๊ทธ๋ž˜์„œ ํ˜น์‹œ ํ•˜๋Š” ๋งˆ์Œ์—, ๋ฌธ์ž์—ด๋กœ ์ €์žฅํ•ด์„œ ํ•œ ๋ฒˆ์— ์ถœ๋ ฅํ–ˆ๋”๋‹ˆ ํ†ต๊ณผ. ๋ฌธ์ œ https://www.acmi

jwonylee.tistory.com

์ด ๋จน๊ตฌ๋ฆ„๋‹˜์˜ ์ฝ”๋“œ๋ฅผ ๋ดค๋Š”๋ฐ, ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ „๋‹ฌํ•ด์„œ m์—์„œ -1 ํ•ด์ฃผ๋Š” ๋ฐฉ์‹์œผ๋กœ ์ง„ํ–‰ํ–ˆ๋‹ค. ๋น„๊ตํ•˜๋Š” ๋ฐฉ์‹๋งŒ ๋‹ฌ๋ผ์กŒ์ง€, ๋‚˜๋ž‘ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋‹ค๋ฅผ๊ฑด ์—†์—ˆ๋‹ค. ์™œ ์ด๊ฑด ํ†ต๊ณผ๋˜๋Š”๊ฑฐ๊ณ  ๋‚ด๊ฐ€ ์ง  ์ฝ”๋“œ๋Š” ํ†ต๊ณผ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒƒ์ผ๊นŒ..

์ฝ”๋“œ๋ฅผ ์กฐ๊ธˆ ๋” ์ž์„ธํžˆ ๋œฏ์–ด๋ณด๋‹ค๊ฐ€, ๋‚ด ์ฝ”๋“œ์— ์žˆ๋Š” 'map' ๊ณ ์ฐจํ•จ์ˆ˜๊ฐ€ ๊ฑฐ์Šฌ๋ฆฌ๊ธฐ ์‹œ์ž‘ํ–ˆ๋‹ค. ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์ž˜๋ชป์“ฐ๋ฉด ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋งค์šฐ๋งค์šฐ ์ปค์ง€๊ธฐ ๋•Œ๋ฌธ์—, ๊ทธ๋Ÿด ๊ฒฝ์šฐ์—๋Š” ๋‚ด๊ฐ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ์ž˜๋ชป์งฐ๋‹ค๊ณ  ๋งํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ์• ํ”Œ ๊ด€๋ จ ์ข…์‚ฌ์ž..ํ•œ ๋ถ„๊ป˜์„œ ๋ง์”€ํ•ด์ฃผ์…จ๋˜๊ฒŒ ๊ธฐ์–ต๋‚ฌ๋‹ค.

๊ทธ๋Ÿผ ๋จน๊ตฌ๋ฆ„๋‹˜์ฒ˜๋Ÿผ, ์• ์ดˆ์— stack array๋ฅผ stringํ˜•ํƒœ๋กœ ๋งŒ๋“ค์–ด์„œ map ๊ณ ์ฐจํ•จ์ˆ˜๋ฅผ ์—†์• ์ฃผ์ž! ๋ผ๋Š”์ƒ๊ฐ์„ ํ–ˆ๋‹ค.. ๊ทธ๋žฌ๋”๋‹ˆ ํ†ต๊ณผ... ํ .. ์ง„์ • map๋•Œ๋ฌธ์ด์—ˆ๋˜๊ฐ€!?! ์• ๋งคํ•˜๋‹ค ใ… ใ…  ์ด๊ฒŒ ๋งž๋‚˜..

๐ŸŸ  ๋‘๋ฒˆ์งธ ํ’€์ด - ๋งž์•˜์Šต๋‹ˆ๋‹ค!

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() {
    if stack.count == m {
    	// โœ… ์—ฌ๊ธฐ์„œ map์„ ์—†์• ์ฃผ์—ˆ๋‹ค.
        answer += stack.joined(separator: " ") + "\n"
        return
    }

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

dfs()
print(answer)

 

๋ฐ˜์‘ํ˜•