1. λ¬Έμ
2. μ μΆλ ₯
3. μ μΆλ ₯ μμ
4. λ¬Έμ μ€κ³
- Nμ΄ 80μ΄λ―λ‘, μμ νμ(Back-tracking)μΌλ‘ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ
- κ°μ₯ μμ μλ₯Ό λνλ΄λ μμ΄λ§ μΆλ ₯νκΈ° μν΄μ κ°μ₯ μμ μμΈ 1λΆν° μ°¨λ‘λ‘ λμμ λ, μ΅μ΄λ‘ μμ±λλ μμ΄μ μΆλ ₯νλ©΄ λλ κ²μ μ μ μμ
- κ° μ리μ리 λ§λ€ 1λΆν° μλ₯Ό λμμΌλ‘μ¨ ν΄λΉ μλ₯Ό ν¬ν¨νλ μ°μλλ λΆλΆμμ μ€λ³΅μ΄ μλ€λ©΄ λ€μ μ«μλ₯Ό λμΌλ©΄ λκ³ , κ·Έλ μ§ μμΌλ©΄ λ€μ λλμμ(Back-tracking) λ€μ μ«μλ₯Ό λμ νλνλ μΌμΌν ν΄λ΄μΌλ‘μ¨ λ¬Έμ λ₯Ό ν΄κ²°ν μ μμ
- ν΄λΉ μλ₯Ό ν¬ν¨νλ μ°μλλ λΆλΆμμ μ€λ³΅μ΄ μλμ§ κ²μ¬νκΈ° μν΄μλ νμ¬ λμμΌ νλ μ«μκ° κ°μ₯ λμ μμΉν΄ μμ κ²μ΄κ³ , μ°μλλ ꡬκ°μ 1λΆν° (νμ¬ μμΉ + 1) / 2 κΉμ§μ ꡬκ°μ κ²μ¬νκ² λλ©΄ μ μ μμ
- κ°λ Ή, [1, 2, ] λ€μμ μ«μλ₯Ό λμμΌ ν κ²½μ°, νμ¬ μμΉλ 2μ΄κ³ , κ΅¬κ° 1λΆν° (2 + 1) / 2 = 1 κΉμ§ μμ λΆν° κ΅¬κ° λ§νΌ λ¨μ΄μ§ κ³³μ μμκ° μ€λ³΅λλμ§λ§ κ²μ¬νλ©΄ λλ κ²μ μ μ μμ
5. μ 체 μ½λ
//MARK: - goodseq
//MARK: - Framework
import Foundation
//MARK: - Variable
var isFinished: Bool = false
//MARK: - Function
func isPossible(_ result: [Int], _ current: Int, _ interval: Int) -> Bool {
var flag: Bool = false
for i in 0..<interval {
if result[current - i] != result[current - i - interval] {
flag = true
break
}
}
return flag
}
func getGoodSeq(_ result: inout [Int], _ N: Int, _ current: Int) -> Void {
if isFinished { return }
if current >= N {
var answer: String = ""
for number in result {
answer += "\(number)"
}
print(answer)
isFinished = true
} else {
for i in 1...3 {
var flag: Bool = false
result[current] = i
for j in stride(from: 1, through: (current + 1) / 2, by: 1) {
if !isPossible(result, current, j) {
flag = true
break
}
}
if !flag {
getGoodSeq(&result, N, current + 1)
}
}
}
}
func solution() -> Void {
//MARK: - Input
guard let N: Int = Int(readLine() ?? "0") else { return }
var result: [Int] = Array(repeating: 0, count: N)
//MARK: - Process & Output
getGoodSeq(&result, N, 0)
}
solution()
μ 체μ½λλ μ¬κΈ°μμ νμΈν μ μμ΅λλ€.
'Swift Data Structure And Algorithm > Recursive Function' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
κ±°λ μ κ³± ꡬνκΈ° L (0) | 2022.04.07 |
---|---|
mountain (0) | 2022.03.14 |
ν©ν λ¦¬μΌ (0) | 2022.03.14 |
binary (0) | 2022.01.29 |