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

Algorithm/Baekjoon

[๋ฐฑ์ค€] (Swift) 1759๋ฒˆ - ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ

๊ฐ์ž ๐Ÿฅ” 2022. 5. 10. 00:54
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ๋งํฌ

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

 

1759๋ฒˆ: ์•”ํ˜ธ ๋งŒ๋“ค๊ธฐ

์ฒซ์งธ ์ค„์— ๋‘ ์ •์ˆ˜ L, C๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. (3 ≤ L ≤ C ≤ 15) ๋‹ค์Œ ์ค„์—๋Š” C๊ฐœ์˜ ๋ฌธ์ž๋“ค์ด ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„๋˜์–ด ์ฃผ์–ด์ง„๋‹ค. ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž๋“ค์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž์ด๋ฉฐ, ์ค‘๋ณต๋˜๋Š” ๊ฒƒ์€ ์—†๋‹ค.

www.acmicpc.net

 

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

  • ๋ชจ์Œ๊ณผ ์ž์Œ์„ ๋ชจ๋‘ ๋ฐฐ์—ด๋กœ ๋งŒ๋“ค์–ด์ค€ ๋’ค, ์—ฌ๊ธฐ์— ๋ชจ์Œ์ด ํ•œ๊ฐœ ์ด์ƒ ํฌํ•จ๋˜๋Š”์ง€์™€ ์ž์Œ์˜ ๊ฐœ์ˆ˜๋ฅผ ๋”ฐ์ ธ์ฃผ์—ˆ๋‹ค.
  • dfs๋ฅผ ์ด์šฉํ•ด์„œ ํ’€์—ˆ๋‹ค.
  • ์ค‘๋ณต์ด ํ—ˆ์šฉ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐฉ๋ฌธ์ฒดํฌ๋ฅผ ํ•ด๊ฐ€๋ฉด์„œ ํ’€์—ˆ๋‹ค.
let LC = readLine()!.split(separator: " ").map{ Int(String($0))! }
let l = LC[0]
let c = LC[1]

var charList = readLine()!.split(separator: " ").map{ String($0) }.sorted(by: <)
var visited = Array(repeating: false, count: c) //c๊ฐœ ๋งŒํผ false ์ƒ์„ฑ (๋ฐฉ๋ฌธ์ฒดํฌ)
var depth = 0 //bfs ๋Œ๋ฆด ๊นŠ์ด
let aeiou = ["a", "e", "i", "o", "u"]
let consonant = ["b","c","d","f","g","h","j","k","l","m","n","p","q","r","s","t","v","w","x","y","z"]
var result: [String] = []


func dfs(depth: Int, start: Int) {
    
    // ์›ํ•˜๋Š” ์ž๋ฆฟ์ˆ˜๋กœ ์•”ํ˜ธ๊ฐ€ ๋งŒ๋“ค์–ด์ง€๋ฉด, ์กฐ๊ฑด ์ฒดํฌ
    if depth == l {
        var containAeiou = false
        var consonantCnt = 0
        
        // ์ตœ์†Œ ํ•œ๊ฐœ ์ด์ƒ, ์กด์žฌํ•˜๋Š”์ง€๋งŒ ํ™•์ธ
        for v in aeiou {
            if result.contains(v) {
                containAeiou = true
            }
        }
        
        // ์ž์Œ ๊ฐœ์ˆ˜ ์„ธ์–ด์ค˜์•ผํ•จ.
        for i in result {
            if consonant.contains(i) {
                consonantCnt += 1
            }
        }
        
        if consonantCnt >= 2 && containAeiou == true {
            print(result.joined(separator: ""))
        }
        return
    }
    
    // dfs
    for i in start..<c{
            if !visited[i] {
                visited[i] = true
                result.append(charList[i])
                dfs(depth: depth + 1, start: i)
                visited[i] = false
                result.removeLast()
            }
        }
}

dfs(depth: depth, start: 0)

 

๋ฐ˜์‘ํ˜•