๊ธ€ ์ž‘์„ฑ์ž: ์ด์ง€์›๐ŸŒฉ๏ธ

์ค‘๋ณต์„ ์ฒ˜๋ฆฌํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ visited ๋ณ€์ˆ˜๋ฅผ ์ด์šฉํ•ด ์ฒดํฌํ–ˆ๊ณ , ์‚ฌ์ „์ˆœ์œผ๋กœ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด ์ด์ „์— ๊ณ ๋ฅธ ์ˆ˜๊ฐ€ ํ˜„์žฌ ๊ณ ๋ฅธ ์ˆ˜๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ์—๋Š” ๊ฑด๋„ˆ๋›ฐ๊ฒŒ ํ–ˆ๋‹ค.


๋ฌธ์ œ

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


๋‚ด๊ฐ€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ

Swift

while let input = readLine() {
    if input == "0" {
        break
    }

    let nums = Array(input.split(separator: " ").map { Int(String($0))! })
    let n = nums.count

    var picked = [String]()
    var visited = Array(repeating: false, count: n)
    func pick(toPick: Int) {
        if toPick == 0 {
            print(picked.joined(separator: " "))
            return
        }

        for i in 1..<n {
            if visited[i] {
                continue
            }
            if !picked.isEmpty && Int(picked.last!)! > nums[i] {
                continue
            }
            visited[i] = true
            picked.append("\(nums[i])")
            pick(toPick: toPick - 1)
            picked.removeLast()
            visited[i] = false
        }
    }

    pick(toPick: 6)
    print()
}
๋ฐ˜์‘ํ˜•