[코딩테스트] 📦 컨베이어 벨트 위 로봇 - 백준 20055
2025. 11. 10. 11:55ㆍ알고리즘
문제링크
난이도: 골드 5
유형: 구현, 시뮬레이션
소요시간: 1시간 + GPT
회고
며칠 안 풀었다고 머리가 굳은 것 같다.
코드
let NK = readLine()!.split(separator: " ").map { Int($0)! }
let (N, K) = (NK[0], NK[1])
var A = readLine()!.split(separator: " ").map { Int($0)! }
var robot = [Bool](repeating: false, count: N)
var start = 0
let size = 2 * N
var step = 0
func beltIndex(_ start: Int, _ posOnTop: Int, _ size: Int) -> Int {
(start + posOnTop) % size
}
while true {
step += 1
// 회전
start = (start - 1 + size) % size
// 로봇 회전
for i in stride(from: N - 1, through: 1, by: -1) {
robot[i] = robot[i - 1]
}
robot[0] = false
robot[N - 1] = false
// 로봇 이동
for i in stride(from: N - 2, through: 0, by: -1) {
if robot[i] && !robot[i + 1] {
let nextIndex = beltIndex(start, i + 1, size)
if A[nextIndex] > 0 {
robot[i] = false
robot[i + 1] = true
A[nextIndex] -= 1
}
}
}
robot[N - 1] = false
let upIndex = beltIndex(start, 0, size)
if !robot[0] && A[upIndex] > 0 {
robot[0] = true
A[upIndex] -= 1
}
if A.filter({ $0 == 0 }).count >= K {
print(step)
break
}
}'알고리즘' 카테고리의 다른 글
| [코딩테스트] 신고결과받기 - Kakao 2022 BLIND (0) | 2025.11.05 |
|---|---|
| [코딩테스트] 🦈 아기 상어 - BJ16236 (0) | 2025.10.22 |
| [코딩테스트] 🔌 전깃줄 - 백준 2565 (0) | 2025.10.20 |
| [코딩테스트] 🦠 연구소 - 백준14502 (0) | 2025.10.20 |
| [코딩테스트] 🧹 로봇청소기 - 백준14503 (0) | 2025.10.20 |