Skip to content

Commit e29e4e5

Browse files
committed
[Gold V] Title: 컨베이어 벨트 위의 로봇, Time: 88 ms, Memory: 69108 KB -BaekjoonHub
1 parent d609e91 commit e29e4e5

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

백준/Gold/20055. 컨베이어 벨트 위의 로봇/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
### 성능 요약
66

7-
메모리: 79512 KB, 시간: 324 ms
7+
메모리: 69108 KB, 시간: 88 ms
88

99
### 분류
1010

1111
구현, 시뮬레이션
1212

1313
### 제출 일자
1414

15-
2025년 3월 14일 21:52:35
15+
2025년 3월 14일 21:53:46
1616

1717
### 문제 설명
1818

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,51 @@
1-
import Foundation
2-
3-
struct Belt {
4-
var robot: Bool // 로봇이 있는지
5-
var duration: Int // 내구도
1+
struct Piece {
2+
init(duration: Int, hasRobot: Bool = false) {
3+
self.duration = duration
4+
self.hasRobot = hasRobot
5+
}
6+
var duration: Int
7+
var hasRobot: Bool
68
}
79

8-
let input = readLine()!.components(separatedBy:" ").map{Int($0)!}
9-
let N = input[0], K = input[1] // 벨트 칸 수, 내구도 0의 수용치
10-
let arr = readLine()!.components(separatedBy: " ").map{Int($0)!}
11-
var belts = [Belt]()
12-
for (i, x) in arr.enumerated() {
13-
belts.append(Belt(robot: false, duration: x))
10+
let nk = readLine()!.split { $0 == " " }.map { Int(String($0))! },
11+
n = nk[0],
12+
k = nk[1]
13+
var conveyerBelt = readLine()!.split { $0 == " " }.map { Int(String($0))! }.map { Piece(duration: $0) }
14+
func rotateConveyer() {
15+
conveyerBelt.insert(conveyerBelt.removeLast(), at: 0)
1416
}
15-
var result = 0
17+
var answer = 0
18+
var zeroBlankCount = 0
1619

17-
while true {
18-
result += 1
19-
// 1
20-
belts.insert(belts.removeLast(), at: 0) // 맨 마지막 벨트를 앞으로
21-
if belts[N-1].robot == true { belts[N-1].robot = false }
22-
// 2
23-
for i in stride(from: N-2, to: -1, by: -1) { // 로봇의 이동
24-
if belts[i].robot == true, belts[i+1].robot == false, belts[i+1].duration > 0 {
25-
belts[i].robot = false
26-
belts[i+1].robot = true
27-
belts[i+1].duration -= 1
28-
if i+1 == N-1 { belts[i+1].robot = false } // N인덱스에 로봇이 위치하면 바로 내려준다.
20+
func countZeroBlank() {
21+
var count = 0
22+
conveyerBelt.forEach { blank in
23+
if blank.duration == 0 {
24+
count += 1
2925
}
3026
}
31-
// 3
32-
if belts[0].robot == false && belts[0].duration > 0 {
33-
belts[0].duration -= 1
34-
belts[0].robot = true
27+
zeroBlankCount = count
28+
}
29+
while zeroBlankCount < k {
30+
answer += 1
31+
rotateConveyer()
32+
if conveyerBelt[n-1].hasRobot {
33+
conveyerBelt[n-1].hasRobot = false
34+
}
35+
for i in stride(from: n-2, through: 0, by: -1) {
36+
if conveyerBelt[i].hasRobot && conveyerBelt[i+1].duration >= 1 && !conveyerBelt[i+1].hasRobot {
37+
conveyerBelt[i+1].duration -= 1
38+
conveyerBelt[i].hasRobot = false
39+
if i == n-2 {
40+
continue
41+
}
42+
conveyerBelt[i+1].hasRobot = true
43+
}
3544
}
36-
// 4
37-
let zeroCount = belts.filter{$0.duration == 0}.count
38-
if zeroCount >= K {
39-
break
45+
if conveyerBelt[0].hasRobot == false && conveyerBelt[0].duration > 0 {
46+
conveyerBelt[0].hasRobot = true
47+
conveyerBelt[0].duration -= 1
4048
}
49+
countZeroBlank()
4150
}
42-
print(result)
51+
print(answer)

0 commit comments

Comments
 (0)