-
Notifications
You must be signed in to change notification settings - Fork 5
/
BOJ_14226.swift
36 lines (29 loc) · 1019 Bytes
/
BOJ_14226.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// 백준 14226 이모티콘
// https://www.acmicpc.net/problem/14226
// 21.09.03
let s = readLine()!.split(separator: " ").map { Int(String($0))! }[0]
var leftQ: [(count: Int, time: Int, clipBoard: Int?)] = [(count: 1, time: 0, clipBoard: nil)]
var rightQ: [(count: Int, time: Int, clipBoard: Int?)] = []
var result = Int.max
while !leftQ.isEmpty || !rightQ.isEmpty {
if rightQ.isEmpty {
rightQ = leftQ.reversed()
leftQ.removeAll()
}
let q = rightQ.popLast()!
let count = q.count
let time = q.time
if time >= result {
continue
} else if count == s {
result = time
} else {
if let clipBoard = q.clipBoard, clipBoard > 0 {
leftQ.append((count: count + clipBoard, time: time + 1, clipBoard: clipBoard))
}
leftQ.append((count: count * 2, time: time + 2, clipBoard: count))
guard count > 0 else { continue }
leftQ.append((count: count - 1, time: time + 1, clipBoard: nil))
}
}
print(result)