Skip to content

Commit 1b6db99

Browse files
committed
BOJ_13335 : 트럭
1 parent e1f5552 commit 1b6db99

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

week2/BOJ_13335(트럭).java

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import java.io.*;
2+
import java.util.*;
3+
4+
public class Main {
5+
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int n = Integer.parseInt(st.nextToken());
10+
int length = Integer.parseInt(st.nextToken());
11+
int maxWeight = Integer.parseInt(st.nextToken());
12+
13+
st = new StringTokenizer(br.readLine());
14+
// [트럭 무게, 현재 위치]
15+
List<int[]> truckList = new ArrayList<>();
16+
for(int i=0; i<n; i++) {
17+
int truck = Integer.parseInt(st.nextToken());
18+
truckList.add(new int[]{truck, -1*i});
19+
}
20+
21+
int time = 0;
22+
int currentWeight = 0;
23+
int count = 0;
24+
int index = 0;
25+
while(true) {
26+
// 마지막 트럭이 다리를 건넜다면 종료
27+
if(truckList.get(n-1)[1] > length) {
28+
System.out.println(time);
29+
return;
30+
}
31+
32+
for(int i=index; i<n; i++) {
33+
int w = truckList.get(i)[0];
34+
int l = truckList.get(i)[1];
35+
if(l > 0) { // 다리 위에 있는 트럭
36+
moveTruck(truckList, i);
37+
}else if(l == 0) { // 다리 입구에 위치한 트럭
38+
// 진입 가능 트럭(무게 중량 초과 x, 다리 길이가 트럭 개수보다 작은 경우)
39+
if((currentWeight + w <= maxWeight) && (count < length)) {
40+
moveTruck(truckList, i);
41+
currentWeight += w;
42+
count++;
43+
}else {
44+
break;
45+
}
46+
}else { // 다리에 진입하지 않은 트럭
47+
moveTruck(truckList, i);
48+
}
49+
50+
// 다리를 모두 건넌 경우
51+
if(l >= length) {
52+
currentWeight -= w;
53+
count--;
54+
index++;
55+
}
56+
}
57+
time++;
58+
}
59+
}
60+
61+
public static void moveTruck(List<int[]> truckList, int index) {
62+
int[] truck = truckList.get(index);
63+
truck[1]++;
64+
truckList.set(index, truck);
65+
}
66+
}

0 commit comments

Comments
 (0)