๋ฐ์ํ
๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/1759
๋ด๊ฐ ํผ ํ์ด
- ๋ชจ์๊ณผ ์์์ ๋ชจ๋ ๋ฐฐ์ด๋ก ๋ง๋ค์ด์ค ๋ค, ์ฌ๊ธฐ์ ๋ชจ์์ด ํ๊ฐ ์ด์ ํฌํจ๋๋์ง์ ์์์ ๊ฐ์๋ฅผ ๋ฐ์ ธ์ฃผ์๋ค.
- 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)
๋ฐ์ํ
'Algorithm > Baekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] (Swift) 14501๋ฒ - ํด์ฌ (DP๋ก ํ๊ธฐ) (0) | 2022.05.17 |
---|---|
[๋ฐฑ์ค] (Swift) 1260๋ฒ - DFS์ BFS (0) | 2022.05.14 |
[๋ฐฑ์ค] (Swift) 15654๋ฒ - N๊ณผ M (5) (DFS๋ก ํ๊ธฐ!) (0) | 2022.05.04 |
[๋ฐฑ์ค] (Swift) 15651๋ฒ - N๊ณผ M (3) (5) | 2022.04.25 |
[๋ฐฑ์ค] (Swift) 1748๋ฒ - ์ ์ด์ด์ฐ๊ธฐ 1 (0) | 2022.04.20 |