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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 15651๋ฒˆ - N๊ณผ M (3)

๊ฐ์ž ๐Ÿฅ” 2022. 4. 25. 22:14
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ ๋งํฌ

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

 

15651๋ฒˆ: N๊ณผ M (3)

ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๋ฌธ์ œ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์ˆ˜์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค. ์ค‘๋ณต๋˜๋Š” ์ˆ˜์—ด์„ ์—ฌ๋Ÿฌ ๋ฒˆ ์ถœ๋ ฅํ•˜๋ฉด ์•ˆ๋˜๋ฉฐ, ๊ฐ ์ˆ˜์—ด์€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด์„œ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค. ์ˆ˜์—ด์€ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ฆ๊ฐ€ํ•˜๋Š” ์ˆœ์„œ๋กœ ์ถœ๋ ฅํ•ด

www.acmicpc.net

 

๋‚ด๊ฐ€ ํ‘ผ ํ’€์ด

  • permutation ์œผ๋กœ ์ง„ํ–‰ํ•˜๋ฉด ๋  ์ค„ ์•Œ์•˜๋Š”๋ฐ ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ๊ฐ™์€ ์ˆ˜ ๋ฐ˜๋ณต์ด ์•ˆ๋๋‹ค.
  • ๊ฐ™์€์ˆ˜๋ฅผ ๋ฐ˜๋ณตํ•ด์„œ ์ถœ๋ ฅํ•ด์ฃผ๊ณ , permutation ๊ฒฐ๊ณผ๋“ค์„ ์ถœ๋ ฅํ•˜๋ ค๊ณ  ํ–ˆ๋Š”๋ฐ ์ƒ๊ฐ์ฒ˜๋Ÿผ ์ž˜ ๋˜์ง€ ์•Š์•˜๋‹ค.
func solution() {
    let arr = readLine()!.split(separator: " ").map{Int(String($0))!}
    let n = arr[0]
    let m = arr[1]


    func nm(_ values: String, _ count: Int) -> Void {
        if count == m {
            print(values)
        } else {
            for i in 1...n {
                nm(values+"\(i) ", count+1)
            }
        }
    }
    for i in 1...n {
        nm("\(i) ", 1)
    }
}

solution()

 

 

์ž˜ ํ’€๋ฆฌ์ง€ ์•Š์•„์„œ ์ธํ„ฐ๋„ท์„ ์ฐธ๊ณ ํ–ˆ๋‹ค.. ์ด๋Ÿฌ๊ฒŒ ํ•˜๋‹ค ๋ณด๋ฉด ๊ฐ์ด ์˜ฌ์ง€,,, ๋„ˆ๋ฌด ์ฐฉ์žกํ•˜๋‹ค!! ํƒ์ƒ‰์ด ์–ด๋ ค์›Œ์„œ ์š” ๋ฉฐ์น  ์ง‘์ค‘๋„๋ชปํ•˜๊ณ  ํ—ค๋งค๊ณ  ์žˆ๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ํ™”๊ฐ€๋‚œ๋‹ค!

๋ฐ˜์‘ํ˜•